java - 尝试 PUT 方法时出现错误 405 (Jersey/Java) : Method Not Allowed

标签 java json jersey put

我在使用 Jersey PUT 方法时遇到问题。我只想将字符串(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 注释。发布您的服务代码以便更好地诊断。

关于java - 尝试 PUT 方法时出现错误 405 (Jersey/Java) : Method Not Allowed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20198290/

相关文章:

java - 如何获取JTable的RowSorter使用的排序键的列?

c# - Azure 生成资源组 C#

javascript - 最佳地将一组字段添加到表单并在提交时生成 JSON

javascript - 如何让服务器返回一个 html 文件?

jersey - 如何强制 URIBuilder.path(...) 对像 "%AD"这样的参数进行编码?此方法并不总是正确地用百分比编码参数

jersey - 如何将 JSON 请求发布到 Jersey REST 服务?

java - 使用 Spring Security 4.0.4 的新 Grails3 网站问题

javax.net.ssl.SSLHandshakeException : sun. security.validator.ValidatorException 错误

java - 在 Yosemite 上运行 brew install elasticsearch 和安装 Java 1.7 时出错

java - 在 Java 中从 JSON 中删除注释的最快方法?