2017-09-04 45 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