简体   繁体   中英

JAVA API , JERSEY / POST not working

So I have in my code POST method :

 @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();

          }

unfortunately its not working. I'm getting 404 error. Server is configured correctly because other methods work perfectly. Funny thing is that when I remove {userPost} , parameter : @PathParam("userPost") String userPost and send empty request : http://localhost:8080/JavaAPI/rest/api/send it works - I'm getting new User object with null at some fields. Do you know why I cannot send parameter ? Thanks in advance for help! :)

What you are sending is not a path parameter to send your value as a path parameter based on your api , let us say you are trying to send "test"

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

if you want to use query params

@POST
  @Path("/send")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces("application/json")
      public Response sendUser(@QueryParam("userPost") String userPost ) {

and your request should be

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

Your "userPost" parameter is not in the Path : localhost:8080/JavaAPI/rest/api/send?=test

You defined this path :

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

So, your URI should be :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM