[英]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.