2013-11-25 126 views
0

我遇到了Jersey PUT方法的問題。我只是想添加一個String(MediaType APPLICATION_JSON)到localhost:8080/testtest。但隨着線嘗試PUT方法(Jersey/Java)時出錯405:方法不允許

service.path("testtest") 
      .type(MediaType.APPLICATION_JSON) 
      .put(String.class, "{\"firstName\":\""+firstName+"\",\"lastName\":\""+lastName+"\"}"); 

嘗試這個,當我得到這個異常:

Exception in thread "main" com.sun.jersey.api.client.UniformInterfaceException: PUT http://localhost:8080/testtest returned a response status of 405 Method Not Allowed 

你有一個想法如何解決這個問題?

這裏是整個代碼:

import java.io.IOException; 
import javax.ws.rs.Path; 
import javax.ws.rs.core.MediaType; 
import com.sun.jersey.api.client.Client; 
import com.sun.jersey.api.client.WebResource; 
import com.sun.jersey.api.container.httpserver.HttpServerFactory; 

@Path("testtest") 
public class TestClass { 

    public static void main(String[] args) throws IllegalArgumentException, IOException { 
     test(); 
    } 

    private static void test() throws IllegalArgumentException, IOException { 
     HttpServer server = HttpServerFactory.create("http://localhost:8080/"); 
     server.start(); 
     WebResource service = Client.create().resource("http://localhost:8080/"); 
     String firstName = "First Name"; 
     String lastName = "Last Name"; 

     service.path("testtest") 
      .type(MediaType.APPLICATION_JSON) 
      .put(String.class, "{\"firstName\":\""+firstName+"\",\"lastName\":\""+lastName+"\"}"); 
     // Exception in thread "main" com.sun.jersey.api.client.UniformInterfaceException: PUT http://localhost:8080/testtest returned a response status of 405 Method Not Allowed 

     System.out.println(service.path("testtest").accept(MediaType.APPLICATION_JSON).get(String.class)); 
     server.stop(0); 
    } 

} 

非常感謝你提前!

回答

2

很可能您的服務方式沒有@PUT註釋。發佈您的服務代碼以獲得更好的診斷。