2017-09-04 47 views
1

所以,我在我的代碼POST方法:JAVA API,汗布/ 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它的工作原理 - 我得到新的用戶對象在一些領域爲null。你知道我爲什麼不能發送參數嗎?預先感謝您的幫助! :)

+1

什麼是你想請求? –

+0

http:// localhost:8080/JavaAPI/rest/api/send?= test – Bartos

回答

2

你所發送不是一個路徑參數來發送你的價值基於您的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 
1

你 「userPost」 參數不在路徑:本地主機:8080 /的JavaAPI/REST/API /發送=測試

你定義這個路徑:

@Path("/send/{userPost}") 

所以,你的URI應該是:

localhost:8080/JavaAPI/rest/api/send/test 
+0

你是什麼意思,它不在路徑中?我覺得是這樣的 ... :) – Bartos