繁体   English   中英

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

[英]Error 405 when trying PUT Method (Jersey / Java): Method Not Allowed

我遇到了Jersey PUT方法的问题。 我只想将一个String(MediaType APPLICATION_JSON)添加到localhost:8080 / testtest。 但是在尝试使用该线时

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注释。 发布您的服务代码以便更好地诊断。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM