所以我的代码中有 POST 方法:
@POST
@Path("/send/{userPost}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces("application/json")
public Response sendUser(@PathParam("userPost") String userPost ) {
List<Post>userPosts = new ArrayList();
Post post = new Post(99,userPost,"Bartek Szlapa");
userPosts.add(post);
User user = new User(99,"Bartek","Szlapa",userPosts);
String output = user.toString();
return Response.status(200).entity(output).build();
}
不幸的是它不工作。我收到 404 错误。服务器配置正确,因为其他方法运行良好。有趣的是,当我删除 {userPost} 时,参数:@PathParam("userPost") String userPost 并发送空请求:http://localhost:8080/JavaAPI/rest/api/send它有效 - 我在某些字段中获得了带有 null 的新用户对象。你知道为什么我不能发送参数吗?在此先感谢您的帮助! :)
最佳答案
您发送的不是基于您的 api 将您的值作为路径参数发送的路径参数,假设您正在尝试发送“测试”
http://localhost:8080/JavaAPI/rest/api/send/test
如果你想使用查询参数
@POST
@Path("/send")
@Consumes(MediaType.APPLICATION_JSON)
@Produces("application/json")
public Response sendUser(@QueryParam("userPost") String userPost ) {
你的要求应该是
http://localhost:8080/JavaAPI/rest/api/send?userPost=test
关于JAVA API,JERSEY/POST 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46033465/