Home > Mobile >  Embedded json object as String in java POJO
Embedded json object as String in java POJO

Time:06-29

My service receive the following request:

@Data
public class InputRequest{
private Information info;
private String response;
}

I am trying to figure out what is the right way to represent the above in the form of JSON.

Note: response can be any jsonObject like

{
"country":"Canada",
"State":"Ontatio"
}

CodePudding user response:

@Data
public class InputRequest{
private Information info;
private Object response;
}

Unless there is some need to work with the string directly later if you map it to just an Object then it can work for both any single Object (including a String) or a multi-item Object like a List<> or Map<> or ....

CodePudding user response:

Hoping service you are referring to Rest service. If you are purely building using JAXRS specifications. Then it can be done easily by converting any Pojo to JSON using javax.ws.rs.core.Response API.

For instance, here MessageData is returning by Response will convert by JSON underlying container.

// pojo web service will return to service consumer
MessageData response = new MessageData();
response.setMessageId(msgId);

// return pojo with response code. 
Response.status(Response.Status.CREATED).entity(response).build();

I've recently written an article on creating and testing REST service using JAX RS specification with Apache TomEE. See if that can be useful for you.

  • Related