bittersour bittersour - 2 months ago 12
Java Question

JAX-RS @POST does not return String

I am trying to write @POST sync method which will take in 2 String parameters. When I access the 2 links from the browser, I did not get the return String "SUCCESS!". It seems the methods doesn't get called at all?

http://localhost:9080/SampleWeb/resources/helloworld/sync?param1=string1&param2=string2

http://localhost:9080/SampleWeb/resources/helloworld/sync/string1/string2

@Path("/helloworld")
public class HelloWorldResource {

@POST
@Produces("text/plain")
@Path("/sync")
public String sync(
@QueryParam("param1") String param1,
@QueryParam("param2") String param2) {

//do something

return "SUCCESS!";
}

@POST
@Produces("text/plain")
@Path("/sync/{param1}/{param2}")
public String sync(
@PathParam("param1") String param1,
@PathParam("param2") String param2) {

//do something

return "SUCCESS!";
}
}

Answer

You said you're accessing the links from a browser - are you "browsing" to the URL, or are you explicitly POSTing to the URL from a form submit (or using javascript)? If you're just "browsing" to the link (i.e. clicking the link or putting it in the address bar), your browser will perform a GET, not a POST.

If you're using a Unix OS or have curl installed on Windows, you can use curl to perform the POST, and you should receive the "SUCCESS!" response:

 curl -X POST http://localhost:9080/SampleWeb/resources/helloworld/sync?param1=string1
Comments