繁体   English   中英

在Jersey中使用POST方法时返回405错误

[英]405 error returned when using POST method in Jersey

我正在使用Jersey,并且GETPUT调用没有问题,但是由于某种原因POST拒绝工作; 我不断收到405。

我已经尝试过对方法中的签名进行多种排列,但我开始怀疑是否还有其他缺失。

这是一个行不通的示例:

服务器端:

@Path("/tmm")

public class TmmRes  {

@POST
@Path("/mypost")
@Consumes(MediaType.APPLICATION_JSON)
public Response postTest(String input) {
    System.out.println("Made it to POST: "+input);
    return Response.status(201).entity(input).build(); 
}

}

客户端:

Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8888/api/v1").path("tmm").path("mypost");
String input = "{\"address\":\"myaddress\",\"user\":4}";
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, input);

我意识到使用JSON应该有一个输入绑定到的对象,但是我降低了复杂性以使事情正常工作。

有人有见识吗? 顺便说一句,我正在使用Tomcat (6.0.43)

我认为您必须像这样创建一个POJO:

public class User {

private String address;
private Integer user;

// getters and setters

}

然后改变

@POST
@Path("/mypost")
@Consumes(MediaType.APPLICATION_JSON)
public Response postTest(String input) {
    System.out.println("Made it to POST: "+input);
    return Response.status(201).entity(input).build(); 
}

至:

@POST
@Path("/mypost")
@Consumes(MediaType.APPLICATION_JSON)
public Response postTest(User input) {
    System.out.println("Made it to POST: "+input);
    return Response.status(201).entity(input).build(); 
}

好的,在朋友的帮助下,我能够弄清楚这一点。 我的网址已重定向。 这导致我的POST变成了GET。 感谢大家的帮助。 您的确认消除了可能性,并帮助我找到了答案。

我在IntelliJ中解决了这个问题。

转到菜单:BUILD-> BUILD ARTIFACTS-> BUILD或REBUILD就是这样。

暂无
暂无

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

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