java api,jersey/post不工作

4xy9mtcn  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(290)

所以在我的代码贴方法中:

@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的新用户对象。你知道为什么我不能发送参数吗?提前感谢您的帮助!:)

kadbb459

kadbb4591#

您发送的不是路径参数,而是基于api将值作为路径参数发送,假设您正在尝试发送“test”

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
hgtggwj0

hgtggwj02#

“userpost”参数不在路径中:localhost:8080/javaapi/rest/api/send?=test
您定义了以下路径:

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

因此,您的uri应该是:

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

相关问题