How to wrap JSON response in a parent object - java

The current response from my Spring REST service is as below :
[
{
"id": "5cc81d256aaed62f8e6462f4",
"email": "exmaplefdd#gmail.com"
},
{
"id": "5cc81d386aaed62f8e6462f5",
"email": "exmaplefdd#gmail.com"
}
]
I want to wrap that in a json object as below :
{
"elements":[
{
"id": "5cc81d256aaed62f8e6462f4",
"email": "exmaplefdd#gmail.com"
},
{
"id": "5cc81d386aaed62f8e6462f5",
"email": "exmaplefdd#gmail.com"
}
]
}
The controller :
#RequestMapping(value = "/users", method = GET,produces = "application/xml")
#ResponseBody
public ResponseEntity<List<User>> getPartnersByDate(#RequestParam("type") String type, #RequestParam("id") String id) throws ParseException {
List<User> usersList = userService.getUsersByType(type);
return new ResponseEntity<List<User>>(usersList, HttpStatus.OK);
}
User model class :
#Document(collection = "user")
public class User {
#Id
private String id;
private String email;
}
How can I implement this?

You could make a new Object to serialize:
class ResponseWrapper {
private List<User> elements;
ResponseWrapper(List<User> elements) {
this.elements = elements;
}
}
Then return an instance of ResponseWrapper in your controller method:
#RequestMapping(value = "/users", method = GET,produces = "application/xml")
#ResponseBody
public ResponseEntity<ResponseWrapper> getPartnersByDate(#RequestParam("type") String type, #RequestParam("id") String id) throws ParseException {
List<User> usersList = userService.getUsersByType(type);
ResponseWrapper wrapper = new ResponseWrapper(usersList);
return new ResponseEntity<ResponseWrapper>(wrapper, HttpStatus.OK);
}

Related

How to add a list to an object in Spring

I have this object class that has a list of customers as an attribute:
#Data
#NoArgsConstructor
#AllArgsConstructor
public class PeopleDTO {
private String processType;
private String operation;
private String entity;
private String entityType;
private Long id;
private Document document;
#Getter
#Setter
class Customer {
private String systemId;
private String customerId;
}
private List<Customer> customers;
}
This list is retrieved calling another microservice using webclient as follows:
public Mono<CuCoPerson> getCuCoPerson(Integer cucoId, String GS_AUTH_TOKEN) {
WebClient webClient = WebClient.create();
return webClient.get()
.uri(GET_RELATION_BY_ID + cucoId)
.header("Accept", "application/json")
.header("Authorization", GS_AUTH_TOKEN)
.retrieve()
.bodyToMono(CuCoPerson.class)
.map(cuCoPerson -> {
List<CustomerRelation> matches = cuCoPerson.getRelatedCustomers()
.stream()
.filter(relation -> relation.getSystemId().equals(400) || relation.getSystemId().equals(300) || relation.getSystemId().equals(410))
.filter(relation -> relation.getCustomerId().contains("F"))
.collect(Collectors.toList());
cuCoPerson.setRelatedCustomers(matches);
return cuCoPerson;
});
}
This method return a cucoPerson as follows:
{
"id": 1,
"relatedCustomers": [
{
"customerId": "xxx",
"systemId": 999
}
]
}
So now I want to add this object to my PeopleDTO class, but I don't know how. This is what I've done son far (hardcoded):
public PeopleDTO createPeople(Long id) {
PeopleDTO people = new PeopleDTO();
people.setProcessType("ONLINE");
people.setOperation("UPDATE");
people.setEntity("DOCUMENT");
people.setEntityType("DOCUMENT");
people.setIdCuco(id);
people.setDocument(new Document());
people.setCustomers(......);
}
So as you can see I don't know how to add a Mono in the last line.
The expected result should be like this:
{
"type": "ONLINE",
"operation": "UPDATE",
"id": 1,
"entity": "DOCUMENT",
"entityType": "NIE",
"documents": {
"id": 1,
"additionals": {
"issuing_authority": "Spain",
"country_doc": "ES",
"place_of_birth": "",
"valid_from": "1995-08-09",
"valid_to": "0001-01-01"
},
"code": "X12345",
"typeDocument": "NIE"
},
"id": 1,
"relatedCustomers": [
{
"customerId": "xxx",
"systemId": 999
}
]
}
first, create a list of customers like:
List<Customer> customers=new ArrayList<>;
Then add all the Customers to it one by one using a loop,
then you can directly add that to your object like
people.setCustomers(customers);
your object assignment should look something like:
public PeopleDTO createPeople(Long id) {
PeopleDTO people = new PeopleDTO();
people.setProcessType("ONLINE");
people.setOperation("UPDATE");
people.setEntity("DOCUMENT");
people.setEntityType("DOCUMENT");
people.setIdCuco(id);
people.setDocument(new Document());
List<Customer> customers=new ArrayList<>;
//add data to customer
people.setCustomers(customers);
}

RabbitMQ Add an object to another one coming from queue in Spring

I have this json object that is mapped to this model class:
Json:
{
"type": "NEW",
"operation": "NEW",
"id": 1,
"entity": "DOCUMENT",
"entityType": "NIE",
"documents": {
"id": 1,
"additionals": {
"issuing_authority": "Spain",
"country_doc": "ES",
"place_of_birth": "",
"valid_from": "1995-08-09",
"valid_to": "0001-01-01"
},
"code": "X12345",
"typeDocument": "NIE"
}
}
The model class:
public class PeopleDocumentDTO {
private String processType;
private String operation;
private String entity;
private String entityType;
private Long id;
private Document document;
#Getter
#Setter
class Customer {
private String systemId;
private String customerId;
}
private List<Customer> customers;
}
The thing is that model class also includes a list of customers that have to be added and its coming from another microservice here:
#Service
public class WebClientService {
public Mono<CuCoPerson> getCuCoPerson(Integer cucoId, String GS_AUTH_TOKEN) {
WebClient webClient = WebClient.create();
return webClient.get()
.uri(GET_RELATION_BY_ID + cucoId)
.header("Accept", "application/json")
.header("Authorization", GS_AUTH_TOKEN)
.retrieve()
.bodyToMono(CuCoPerson.class)
.map(cuCoPerson -> {
List<CustomerRelation> matches = cuCoPerson.getRelatedCustomers()
.stream()
.filter(relation -> relation.getSystemId().equals(400) || relation.getSystemId().equals(300) || relation.getSystemId().equals(410))
.filter(relation -> relation.getCustomerId().contains("F"))
.collect(Collectors.toList());
cuCoPerson.setRelatedCustomers(matches);
return cuCoPerson;
});
}
}
Last but not least, this is my listenerClass where I map my message:
#RabbitListener(queues = "${event.queue}")
public void receivedMessage(Message message) throws JsonProcessingException {
String json = "";
json = new String(message.getBody(), StandardCharsets.UTF_8);
System.out.println(json);
logger.info("Received message: {}", json);
ObjectMapper objectMapper = new ObjectMapper();
PeopleDocumentDTO dto = objectMapper.readValue(json, PeopleDocumentDTO.class);
So, how can I add this cuCoPerson to my model DTO?

How to parse a single json object responses?

I am trying to parse/convert a get response to a POJO. I have the User class below and the Controller class.
In the getUsers() method, I am able to parse the request by using ParameterizedTypeReference with wrapper class. However, I am not able to parse the "single object" response.
For getUser response, if I use the same code by changing the ParameterizedTypeReference<UsersWrapper>() as ParameterizedTypeReference<User>() or use the code shown in the getUser method, I get null values for my User class.
I assume I am not able to parse the JSON because of the { "user": {.. wrapper.
How can I have the values for the User class from JSON by using:
#GetMapping("/{id}")
public User getUser(#PathVariable long id) {}
JSON:
{
"user": {
"id": 375607441100,
"url": "https://example.com/users/375607441100.json",
"name": "example-user",
"email": "example-user#hotmail.com",
"created_at": "2020-11-25T09:13:40Z",
"updated_at": "2020-11-25T09:16:44Z",
"time_zone": "Europe/Warsaw",
"iana_time_zone": "Europe/Warsaw",
"phone": null,
"shared_phone_number": null,
"photo": null,
"locale_id": 1,
"locale": "en-US",
"organization_id": null,
"role": "admin",
"verified": true,
"external_id": null,
"tags": [],
"alias": null,
"active": true,
"shared": false,
"shared_agent": false,
"last_login_at": "2020-11-25T09:16:35Z",
"two_factor_auth_enabled": null,
"signature": null,
"details": null,
"notes": null,
"role_type": null,
"custom_role_id": null,
"moderator": true,
"ticket_restriction": null,
"only_private_comments": false,
"restricted_agent": false,
"suspended": false,
"chat_only": false,
"default_group_id": 360005501980,
"report_csv": false,
"user_fields": {}
}
}
User class:
#Data
public class User {
private long id;
private String name;
private String email;
private String role;
}
UsersWrapper:
#Data
#AllArgsConstructor
public class UsersWrapper {
private List<User> users = new ArrayList<>();
public UsersWrapper() {
}
}
Controller getUsers():
#GetMapping
public List<User> getUsers() {
UsersWrapper claims = new UsersWrapper();
try {
ResponseEntity<UsersWrapper> claimResponse = restTemplate.exchange(
resourceUrl,
HttpMethod.GET,
request(), // headers with basicAuth -> username:token
new ParameterizedTypeReference<UsersWrapper>() {
});
if (claimResponse != null && claimResponse.hasBody()) {
claims = claimResponse.getBody();
}
} catch (
RestClientException e) {
e.printStackTrace();
}
List<User> users = claims.getUsers();
return users;
}
getUser():
#GetMapping("/{id}")
public User getUser(#PathVariable long id) {
String url = resourceUrl + "/" + id;
ResponseEntity<User> response = restTemplate.exchange(
url,
HttpMethod.GET,
request(), User.class);
User user = response.getBody();
return user;
}
Creating the following wrapper class with #JsonProperty("user") solved the case.
#Data
#AllArgsConstructor
public class UserWrapper {
private UserDTO user;
public UserWrapper() {
}
#JsonProperty("user")
public UserDTO getUser() {
return user;
}
}

Mapping complex JSON objects in RestTemplate

The structure of JSON looks like:
{
"id": "0001",
"type": "portable",
"name": "mobile",
"results":[
{
"company": "Apple",
"country": "US",
"city": "Cupertino"
},
{
"company": "Google",
"country": "Japan",
"city": "Tokyo"
}
]
}
I tried to map the above json to my "Response" class but I could only retrieve id, type and name. For json object "results" is shows results:[].
Here is the code I have implemented;
CompanyDetail class:
#JsonIgnoreProperties(ignoreUnknown = true)
public class CompanyDetail {
public String company;
public String country;
public String city;
//getters and setters
}
Company class:
#JsonIgnoreProperties(ignoreUnknown = true)
public class CompanyD {
public String name;
public List<Detail> result;
//getters and setters
}
Implementation class:
#Override
public ResponseEntity<String> getResponseEntity() {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate .getForEntity(
"url", String.class);
logger.info("Response:"+response);
return response;
}
#Override
public Company getResponseObject() {
RestTemplate restTemplate=new RestTemplate();
Company comp=(Company) restTemplate.getForObject("url",
Company.class,200);
List<CompanyDetail> companyInfo = comp.getCompanyDetail();
for (CompanyDetail companyDetail : companyInfo) {
logger.info("Country:"+companyDetail.getCountry());
logger.info("City:"+companyDetail.getCity());
logger.info("Company:"+companyDetail.getCompany());
}
return comp;
From both the implementation method I am getting null values for the "results" json object. I could not figure out what is wrong with my code.
Thanks in advance.
I'm having the same issue however in your case it looks like you misspelled "resluts".

Spring hateoas how to set relationship and have array for single link also

Can someone please let me know how to get below response using hateoas
{
"ticketID" : 345,
"links": [ {
"rel": "self",
"href": "rest/INL_TM/rest/v1/ticket/345"
} ]
}
But I am getting response as
{
"ticketID" : 345,
"links": {
"rel": "self",
"href": "rest/INL_TM/rest/v1/ticket/345"
}
}
Also how do I set rel to custom value? I want something like rel = "getTicket"
Greeting.java
public class Greeting extends ResourceSupport {
private final String content;
#JsonCreator
public Greeting(#JsonProperty("content") String content) {
this.content = content;
}
public String getContent() {
return content;
}
}
GreetingController.java
#Controller
public class GreetingController {
private static final String TEMPLATE = "Hello, %s!";
#RequestMapping("/greeting")
#ResponseBody
public HttpEntity<Greeting> greeting(
#RequestParam(value = "name", required = false, defaultValue = "World") String name) {
Greeting greeting = new Greeting(String.format(TEMPLATE, name));
greeting.add(linkTo(methodOn(GreetingController.class).greeting(name)).withSelfRel());
return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);
}
}
Thanks in advance
extend ResourceSupport in model class.
Return Resource instead of ResponseEntity
GreetingController.java
#Controller
public class GreetingController {
#RequestMapping("/greeting")
#ResponseBody
public Resource<Greeting> greeting(
#RequestParam(value = "name", required = false, defaultValue = "World") String name) {
Resource<Greeting> resourceResponse = new Resource<Greeting>("Some String");
resourceResponse.add(linkTo(methodOn(GreetingController.class).greeting("Some String")).withSelfRel());
return resourceResponse;
}
}
Maven dependency
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>0.16.0.RELEASE</version>
</dependency>

Categories

Resources