I have a REST service endpoint that accepts JSON in the following format:
{
"name": "example",
"properties": {
"key1": "val1",
"key2": "val2"
}
}
Then I have this class
class Document {
private String name;
private Map<String, String> properties;
... setters/getters
}
And my REST service method
logger = Logger.getLogger("logger");
#POST
#Consumes(MediaType.APPLICATION_JSON)
Response addDocument(Document document) {
logger.info(document);
return Response.ok(200).build();
}
But whenever I post the JSON above it's not unmarshalling my properties. It is always null. The name property is mapped correctly..
I would gladly accept any help/clues.Thanks!
EDIT: I did not mention that I am using glassfish 4.1 and what comes with it. I don't have any lib dependency for marshal/unmarshal.
I just added Gson and created custom entity provider for JSON. It works now as expected, but I would've been more glad to not handle it myself but let the container do it with whatever is configured for JAXB.
Your JSON, the Java class the JSON will be parsed into and your JAX-RS resource method look fine:
{
"name": "example",
"properties": {
"key1": "val1",
"key2": "val2"
}
}
public class Document {
private String name;
private Map<String, String> properties;
// Getters and setters omitted
}
#POST
#Consumes(MediaType.APPLICATION_JSON)
public Response addDocument(Document document) {
...
return Response.ok(200).build();
}
Instead of using Gson as you mentioned in your answer, you could consider using a JSON provider which integrates with Jersey. Jackson is a good choice and Maps should work fine.
For details on how to use Jackson with Jersey, refer to this answer.
Related
I am developing a restful web application using java and jax-rs. In server side, I have to accept Json type requests as given by the client using POST method.
#POST
#Path("/Request")
#Consumes(MediaType.APPLICATION_JSON)
public String process(SMessage message) throws Exception {
message.getHeader();
message.getBody();
}
The json object have two composite fields like in the below format.
{
"header": {
"serviceType": "A"
},
"body": {
"id": "1",
"name": "abc"
}
}
Based on the serviceType specified in the Header field, the parameters in the body will differ. I need to map this body field in to a java POJO class. The POJO class is not the same for all requests. How can I achieve this effectively?
If you are using Jackson as your JSON parser, a simple way is to declare body to be a JsonNode to handle dynamic keys as follows:
class MyResponse {
private MyHeader header;
private JsonNode body;
//general getters and setters
}
And I assumed that you have already a POJO for header such as:
class MyHeader {
private String serviceType;
//general getters and setters
}
Finally, let's verify that it works:
String responseStr = "{\"header\": {\"serviceType\": \"A\"},\"body\": {\"id\": \"1\",\"name\": \"abc\"}}";
ObjectMapper mapper = new ObjectMapper();
MyResponse myResponse = mapper.readValue(responseStr, MyResponse.class);
System.out.println(myResponse.getBody().get("name").asText());
Console output:
abc
Another way to solve this issue is by using Map and everything else is the same:
class MyResponse {
private MyHeader header;
private Map<String, Object> body;
//general getters and setters
}
Need to map multiple types of JSON responses to a single POJO so that I can compare the different objects to provide insight about the differences.
I had tried mapping the first response to the POJO and parsed the second response to populate the defined POJO:
class XXX {
#JsonProperty("accountHolder")
private String accountHolder;
#JsonProperty("routingNumber")
private String routingNumber;
#JsonProperty("balance")
private List<Balance> balance;
#JsonProperty("accountName")
private String accountName;
#JsonProperty("bankTransferCodeType")
private String bankTransferCodeType;
#JsonProperty("individualInformation")
private IndividualInformation individualInformation;
#JsonProperty("acctType")
private String acctType;
#JsonProperty("transactionList")
private TransactionList transactionList;
#JsonProperty("accountNumber")
private String accountNumber;
#JsonProperty("uniqueId")
private String uniqueId;
#JsonProperty("bankNetID")
private String bankNetID;
#JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
}
First response:
[
{
"ACCOUNT_NAME": "",
"ACCOUNT_NUMBER": "",
"AVAILABLE_BALANCE": null,
"CURRENT_BALANCE": "",
"FULL_ACCOUNT_NUMBER": null,
}
]
Second response:
"bankAccount": [
{
"accountName": "",
"accountNumber": "",
"routingNumber": "",
"fullAccountNumber": "",
"bankTransferCodeType": "",
"acctType": "",
"transactionList": {
"transaction": [
{
"amount": {
"curCode": "",
"content": ""
}
],
"oldestTxnDate": ""
},
"uniqueId":
}
}
Expecting a generic way to map the different structured JSON entities to single POJO.
How to map multiple JSON responses to a single Java POJO?
As both responses seem to be completely different from each other, with nothing in common, I would refrain from attempting to use a single class for reading both responses.
Expecting a generic way to map the different structured JSONs to single POJO.
You could parse both responses as a Map<String, Object> and then map the values to a common class.
You could create separated classes for mapping each response. It will allow you to decouple them and evolve them as you need. You also can use use mapping frameworks such as MapStruct for reducing the boilerplate code when mapping from one object to another.
It doesn’t seems to have any generic way. But you can do this:
Create multiple domain classes for each response type
Create a single standard domain class
Create mapper for each response class to map that to standard domain
class. You can use MapStruct reference here
I would suggest using Jackson Json Views. Here is an example for the same :
Example
public class Views {
public class Global {
}
public class Internal extends Global {
}
}
class XXX {
#JsonView(Views.Global.class)
#JsonProperty("accountHolder")
private String accountHolder;
#JsonView(Views.Internal.class)
#JsonProperty("routingNumber")
private String routingNumber;
}
Hope it helps.
What I did is I created a MyResponse model containing basically all response fields from the JSON response you expect to get.
MyResponse has c-tor or receiving these fields or setters allowing setting them.
Then I created some kind of service class MyService that can issue multiple requests and gets responses back.
Then you just do something like this in some kind of manager class or whatever you call it:
MyService mySer = new MyService();
MyResponse myRes = new MyResponse(
mySer.getDetails(),
mySer.getPicture(),
mySer.getSomethingElse()
);
These calls (getDetails, getPicture...) send requests to end point and return responses which are then just mapped into the the fields of MyResponse class constructor. This happens by the framework so MyResponse has annotations #XmlRootElement and #XmlAccessorType of type FIELD to ensure that happens.
If for whatever reason, you dont want to create response containing result of getPicture for example, you just assign null to that imput parameter.
I suggest to use #JsonProperty("") and #JsonAlias("").
class XXX {
#JsonAlias("accountName")
#JsonProperty("ACCOUNT_NAME")
private String name;
#JsonAlias("routingNumber")
#JsonProperty("ROUTING_NUMBER")
private String routing;}
I hope it helps.
I know this has been asked before, but going through multiple answers did not help me solve my issue. So here it is: I'm a newbie trying to create a REST service using Apache CXF. I'm trying to write a POST method and send the data as JSON in the request body(using POSTMAN in Google Chrome to do this).
My interface looks something like this:
#Path("/")
#Produces("application/json")
public interface MyService{
#POST
#Path("/addNote/{id}")
#Consumes("application/json")
NoteResponse addNote(#PathParam("id") Long id, #QueryParam("note")Note note);
// OTHER #GET METHODS THAT WORK WELL
}
My implmentation:
#WebService(name = "testservice")
public class MyServiceImpl implements MyService{
#Override
public NoteResponse addNote(Long id, Note note){
// SOME IMPLEMENTATION
}
// OTHER #GET METHOD IMPLEMENTATIONS THAT WORK
}
I've read in some answers that I do not need the #QueryParam on my note annotation, instead just put and #XMLRootElement on my Note class, but doing that and trying going on localhost:8080/rest/addNote/1 will NOT call my addNote method.
The problem I am facing now is that the note parameter comes null.
Here's the JSON I've sent via POSTMAN:
{
"note":{
"id": 4958,
"anotherId": 7886,
"comment": "salut",
"mail": "mail#mail.com",
"gregorianDate": "01-01-2016",
"type": "INFO"
}
}
Please try changing your interface definition of API addNote to this:
NoteResponse addNote(#PathParam("id") Long id, Note note);
And send this JSON string via POSTMAN:
{
"id": 4958,
"anotherId": 7886,
"comment": "salut",
"mail": "mail#mail.com",
"gregorianDate": "01-01-2016",
"type": "INFO"
}
This should work.
I am getting the below JSON response format from a third party web service:
{
"meta": {
"code": 200,
"requestId": "1"
},
"response": {
"locations": [
{
"id": "1",
"name": "XXX",
"contact": {
phone: '123',
email: 'abc'
},
"location": {
"address": [
"Finland"
]
}
},
{
// another location
}
]
}
}
And here is what I should return as a response from my own web service:
[
{
"id": "1",
"name": "XXX",
"phone": '123',
"address": "Finland"
},
{
// another location
}
]
What should I do? I've read some good stuff about Jackson but there are only a few simple examples where you map some simple JSON obj as is to POJO. In my case, I need to remove a few nodes, and also traverse deeper down the hierarchy to get the nested value. This is my baby step so far in my spring boot app:
#GET
#Path("{query}")
#Produces("application/json")
public String getVenues(#PathParam("query") String query){
return client.target(url).queryParam("query",query).request(...).get(String.class)
}
Any helps, pointers, recommendations are welcomed!
You are using JAX-RS annotations instead of the Spring web service annotations. You can make this work, but I would recommend going with the default Spring annotations because those are all autoconfigured for you if you're using the spring boot starter dependencies.
First thing - you need to create classes that are set up like the request and response. Something like this:
public class ThirdPartyResponse {
MetaData meta;
Response response;
}
public class Response {
List<Location> locations;
}
public class MetaData {
String code;
String requestId;
}
public class Location {
String id;
String name;
Contact contact;
LocationDetails location;
}
public class Contact {
String phone;
String email;
}
public class LocationDetails {
List<String> address;
}
You can use Jackson annotations to customize the deserialization, but by default it maps pretty logically to fields by name and the types you might expect (a JSON list named "locations" gets mapped to a List in your object named "locations", etc).
Next you'll want to use a #RestController annotated class for your service, which makes the service call to the third party service using RestTemplate, something like:
#RestController
public class Controller {
#Value("${url}")
String url;
#RequestMapping("/path"
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public List<Location> locations(#RequestParam String query) {
// RestTemplate will make the service call and handle the
// mapping from JSON to Java object
RestTemplate restTemplate = new RestTemplate();
ThirdPartyResponse response = restTemplate.getForObject(url, ThirdPartyResponse.class);
List<Location> myResponse = new List<>();
// ... do whatever processing you need here ...
// this response will be serialized as JSON "automatically"
return myResponse;
}
}
As you can see, Spring Boot abstracts away a lot of the JSON processing and makes it pretty painless.
Take a look at Spring's guides which are pretty helpful:
Consuming a service with RestTemplate
http://spring.io/guides/gs/consuming-rest/
Creating a web service using #RestController
https://spring.io/guides/gs/rest-service/
This can be done using google api JacksonFactory.
Below is my suggested solution for this.
First you should create a pojo class corrosponding to the json data you are recieving and the json data to which you are trying to convert.
Use google api client to map the keys to the pojo.
Below is the pojo classes corrosponding to the json data you are recieving.
import com.google.api.client.util.Key;
Class Response{
#Key("locations")
List<FromLocations> fromLocations;
}
import com.google.api.client.util.Key;
Class FromLocations
{
#Key("id")
String id;
#Key("name")
String name;
#Key("contact")
Contact contact;
#Key("location")
Location location;
}
Here Contact and Loaction will be a another classes using the same strategy;
Below is the pojo corrosponding to the json to which you want to convert.
Class ToLocations{
String id;
String name;
String phone;
String address;
}
Now you can parse the requset containing the json objec to the fromLocations class as below.
String responseMeta = response.parseAsString();
JSONObject queryJsonObject = new JSONObject(responseMeta);
if(queryJsonObject.has("locations")){
Response response = JacksonFactory.getDefaultInstance().fromString(responseMeta,Response.class);
List<FromLocations> fromLocationsList = response.getFromLocations();
}
Next step is to iterate the list fromLocationsList and get the desired values from each FromLocations object and add it to the ToLocations object.
Next the ToLocations object can be add it to a list and convert it to json.
I have the following controller:
#RequestMapping(value = "/test/inst/", method = RequestMethod.POST)
public #ResponseBody ResponseDTO<InstDTO>
testPostREST(#RequestBody RequestDTO<InstDTO> instDTO) {
RequestDTO<InstDTO> dto = new RequestDTO<InstDTO>();
ResponseDTO<InstDTO> responseDto = new ResponseDTO<InstDTO>();
responseDto.setPayload(instDTO.getPayload());
return responseDto;
}
with the following request object:
#JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class RequestDTO<T> {
private List<T> payload;
public RequestDTO() {
System.out.println("constructor");
}
public RequestDTO(List<T> payload) {
this.payload = payload;
}
public List<T> getPayload() {
return payload;
}
public void setPayload(List<T> payload) {
this.payload = payload;
}
}
When the POST comes through and I look the object I get, the payload list has LinkedHashMap objects instead of objects of my DTO type.
How can I make spring+jackson convert the JSON into my DTO object. Bear in mind that I plan to reuse the wrapper ResponseDTO for other lists of objects and that's why I'm using a generic list (List).
Here's the JSON I'm trying.
{
"payload": [
{
"d": "Test 0",
"id": "abcde",
"c": "Test 0"
},
{
"d": "Test 1",
"id": "123",
"c": "Test 1"
}
]
}
Your code does not work due to type erasure. Jackson runtime does not know that you are trying to marshal the payload into InstitutionDTO objects (since this information has been erased at compile time). When Jackson looks at the payload, it sees valid JSON objects on the wire and a List<Object> on the Java side. It has no choice but to map the payload to a Map implementation, which in your case seems to be LinkedHashMap.
You may be able to achieve what you are looking for as follows:
class RequestDTO<T> {}
class ResponseDTO<T> {}
class InstitutionDTO {}
class InstitutionRequestDTO extends RequestDTO<InstitutionDTO>
class InstitutionResponseDTO extends ResponseDTO<InstitutionDTO>
#RequestMapping(value = "/test/institution/", method = RequestMethod.POST)
public #ResponseBody InstitutionResponseDTO
testPostREST(#RequestBody InstitutionRequestDTO institutionDTO) { }
Disclaimer: I haven't tried this code myself but most of my applications have code similar to this and it works with Jackson, Castor, Atom, etc. without a glitch.
If using Jackson isn't a requirement then you might want to consider using Gson instead.
You can tell Gson how to deserialize your json by just passing it a json string and the class that you want to deserialize. It would be pretty simple. I've included an example where I had to deserialize a response from a rest api below.
entity = response.getEntity();
jsonResponse = EntityUtils.toString(entity);
JsonNode root = mapper.readTree(jsonResponse).get("result");
returnedProduct = gson.fromJson(root.toString(),Product.class);