AkD
AkD

Reputation: 437

Way to Consume Json request as Json object in Jersey Rest Service

Hi I tried googling around but cannot find solution that I want to achieve.

Example to map json to java object we do

 @POST
 @Consumes(application/json)
 @Produces(application/json)
 public Response createUpdateDeleteManualClinicalData(MyJavaPojo definedPojo) {
   // this maps any json to a java object, but in my case I am dealing with generic json structure
}

What I want to achieve is Keep it as json object itself

public Response createUpdateDeleteManualClinicalData(JSONObject json)

Work around: I can get data as plain text and convert that to json. But its an overhead from my side which I want to avoid.

Edit: I am open to using any Json library like JsonNode etc... as far as I get Json object Structure directly without the overhead of String to Json from my side. This should be common usage or am I missing some core concept here.

Upvotes: 0

Views: 9869

Answers (3)

AkD
AkD

Reputation: 437

It was a straight solution that I happened to overlooked... my bad. Jersey is way smarter than I thought... curious to know what magic happens under the layers

@POST
 @Consumes(application/json)
 @Produces(application/json)
 public Response createUpdateDeleteManualClinicalData(JsonNode jsonNode) {
//jsoNode body casted automatically into JsonNode
}

Upvotes: 2

Ian Lim
Ian Lim

Reputation: 4274

If you are using the same configuration as web.xml setup gists for Jersey1 and Jackson2, then you may do as below. This is a possible alternative than using JSONObject

@POST
@Path("/sub_path2")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Map<String,Object> saveMethod( Map<String,Object> params  ) throws IOException {

    // Processing steps

    return params;
}

Upvotes: 0

Ian Lim
Ian Lim

Reputation: 4274

What is the web.xml configuration you are using? Is it something similar to here web.xml setup gists for Jersey1 and Jackson2?

Upvotes: 0

Related Questions