首页 文章

JAVA API,JERSEY / POST无效

提问于
浏览
1

所以我在我的代码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错误 . 服务器配置正确,因为其他方法完美运行 . 有趣的是,当我删除时,参数:@PathParam("userPost")字符串userPost并发送空请求:http://localhost:8080/JavaAPI/rest/api/send它的工作原理 - 我在某些字段中获取带有null的新User对象 . 你知道为什么我不能发送参数吗?在此先感谢您的帮助! :)

2 回答

  • 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”参数不在路径中:localhost:8080 / JavaAPI / rest / api / send?= test

    您定义了此路径:

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

    所以,你的URI应该是:

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

相关问题