Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试PUT方法(Jersey/Java)时出现错误405:不允许使用方法_Java_Json_Jersey_Put - Fatal编程技术网

尝试PUT方法(Jersey/Java)时出现错误405:不允许使用方法

尝试PUT方法(Jersey/Java)时出现错误405:不允许使用方法,java,json,jersey,put,Java,Json,Jersey,Put,我对Jersey PUT方法有问题。我只想向localhost:8080/testtest添加一个字符串(mediatypeapplication_JSON)。但是当你试着用这条线的时候 service.path("testtest") .type(MediaType.APPLICATION_JSON) .put(String.class, "{\"firstName\":\""+firstName+"\",\"lastName\":\""+las

我对Jersey PUT方法有问题。我只想向localhost:8080/testtest添加一个字符串(mediatypeapplication_JSON)。但是当你试着用这条线的时候

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);
    }

}

提前非常感谢

您的服务方法很可能没有
@PUT
注释。发布您的服务代码,以便更好地诊断