[英]JAVA API , JERSEY / POST not working
所以我在我的代码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”)字符串userPost并发送空请求: http:// localhost:8080 / JavaAPI / rest / api / send it works - 我正在变新用户对象在某些字段处为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
您的“userPost”参数不在路径中:localhost:8080 / JavaAPI / rest / api / send?= test
您定义了此路径:
@Path("/send/{userPost}")
所以,你的URI应该是:
localhost:8080/JavaAPI/rest/api/send/test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.