Chit Khine Chit Khine - 5 months ago 16x
Java Question

@Post in Rest Service accepting Json Values

I have been trying to learn @POST to create webservices using Jersey.
I can now fully adapt the @Get and @Post using the normal params.
For example,

public Response postMsg(@PathParam("param") String msg) {
String output = "POST:Jersey say : " + msg;
return Response.status(200).entity(output).build();

However, I am not sure how I am suppose to accept JSON message and either create or process it through the url.

Any examples would be appreciated.


Create a Data Transfer Object which will be used to consume JSON send from UI and use it directly. @Consume can be used to configure data type format send from UI It can be JSON or Form submit etc

public Response postMsg(UserData userData) {

public class UserData {

    private String userName;
    private String userAddress;
    define getter setter 


// UI JSON call 

    var url = "rest/employee";
    var type = "POST";
    var data = {
        userName : $("#txtEmpFirstName").val(),
        userAddress : $("#txtEmpLastName").val(),

        type : type,
        url : url,
        data : JSON.stringify(data),
        contentType: "application/json",
        success : function(data) {
        }, error : function(error){ alert(JSON.stringify(error.responseText));}