Could not extract response in RestTemplate - java

I have a SpringBoot app. with this config file:
#Configuration
public class ApplicationConfig {
#Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM));
restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
return restTemplate;
}
}
and this class:
#Builder
#Data
#NoArgsConstructor
#AllArgsConstructor
#JsonInclude(NON_NULL)
public class GeolocationAddress {
private Integer placeId;
private String licence;
private String osmType;
private Integer osmId;
private List<String> boundingbox = null;
private String lat;
private String lon;
private String displayName;
private String _class;
private String type;
private Double importance;
private Address address;
}
and this service:
public GeolocationAddress searchFromAddress(String address) {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>(headers);
return restTemplate.exchange("http://nominatim.openstreetmap.org/search?" + address, HttpMethod.GET, entity, GeolocationAddress.class).getBody();
}
but I have this error when running the service:
org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.bonansa.domain.GeolocationAddress] and content type [text/html]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:126)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:998)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:981)

It appears you’re missing format query param based on api doc.
In your case i think it should be format=json
https://nominatim.org/release-docs/develop/api/Search/
Example copies from doc - JSON with address details
https://nominatim.openstreetmap.org/?addressdetails=1&q=bakery+in+berlin+wedding&format=json&limit=1
{
"address": {
"bakery": "B\u00e4cker Kamps",
"city_district": "Mitte",
"continent": "European Union",
"country": "Deutschland",
"country_code": "de",
"footway": "Bahnsteig U6",
"neighbourhood": "Sprengelkiez",
"postcode": "13353",
"state": "Berlin",
"suburb": "Wedding"
},
"boundingbox": [
"52.5460929870605",
"52.5460968017578",
"13.3591794967651",
"13.3591804504395"
],
"class": "shop",
"display_name": "B\u00e4cker Kamps, Bahnsteig U6, Sprengelkiez, Wedding, Mitte, Berlin, 13353, Deutschland, European Union",
"icon": "https://nominatim.openstreetmap.org/images/mapicons/shopping_bakery.p.20.png",
"importance": 0.201,
"lat": "52.5460941",
"licence": "Data \u00a9 OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright",
"lon": "13.35918",
"osm_id": "317179427",
"osm_type": "node",
"place_id": "1453068",
"type": "bakery"
}

Related

Not able to deserialize nested JSON array Spring Resttemplate

I am not able to deserialize nested JSON array from response JSON using Spring Rest template.
JSON response which I am consuming is as follows
[
{
"creationTime": "2023-01-13",
"code": "456",
"cid": "123",
"priority": "CRITICAL",
"reviewDate": null,
"systemCall": [
{
"creationTime": "2023-01-13",
"status": null,
"id": "787878",
"modificationTime": "2023-01-13",
"creatorId": "ABC"
},
{
"creationTime": "2023-01-14",
"status": null,
"id": "787879",
"modificationTime": "2023-01-14",
"creatorId": "DEF"
}
],
level: "1"
}
]
And My model classes as follows
public class Resolution {
private String creationTime;
private String code;
private String cid;
private String priority;
private String reviewDate
private List<SystemCallVo> systemCall;
private String level;
public Resolution(){
}
//Getters and Settrs
}
public class SystemCallVo {
private String creationTime;
private String status;
private String id;
private String modificationTime;
private String creatorId;
public SystemCallVo(){
}
//Getters and Setters
}
public class ResolutionVo extends Resolution{
public ResolutionVo(){
}
}
I am calling my endpoint using rest template as follows.
ResponseEntity<List<ResolutionVo>> response = this.restTemplateConfig.restTemplate().exchange(builder.toUriString(), HttpMethod.POST, httpEntity, new ParameterizedTypeReference<List<ResolutionVo>>() {
}, new Object[0]);
Problem is List systemCall object is always null in response received through resttemplate even though systemCall attribute is present in JSON whenever I hit endpoint through swagger.
There is a defect in RestTemplate.exchange that prevents the deserialisation of even moderately complex JSON objects.
Read the response as a String and then deserialise to List<ResolutionVo> with an com.fasterxml.jackson.databind.ObjectMapper instance as follows:
ResponseEntity<String> response = this.restTemplateConfig.restTemplate().exchange(builder.toUriString(), HttpMethod.POST, httpEntity, String.class, new Object[0]);
String body = response.getBody();
List<ResolutionVo> value = objectMapper.readValue(body, new TypeReference<List<ResolutionVo>>() {});
I think this is a related issue.

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?

Having null response for a list of objects in REST API call with RestTemplate

So I'm working on a REST client that consumes a REST API to get a JSON object using the Spring RestTemplate. So I get an HTTP 200 OK response but the list (equipment) inside the class object is null. But other fields are fetched. When I do the same request using the Postman it works well. What might be the reason for this?
The RestTemplate code snippet :
RestTemplate restTemplate = new RestTemplate();
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Content-Type", "application/json");
requestHeaders.add("Authorization", "Bearer " + apiToken);
HttpEntity entity = new HttpEntity(requestHeaders);
ResponseEntity<CloverMerchant> response = restTemplate.exchange(getMerchantDetailsUrl, HttpMethod.GET, entity, CloverMerchant.class);
return response.getBody();
The CloverMerchant model class :
private String id;
private String name;
private String website;
private boolean isBillable;
private CloverBusinessEquipments equipment;
// other relevant getters and setters
The CloverBusinessEquipments model class:
private List<CloverBusinessEquipment> elements;
public CloverBusinessEquipments() {
}
#JsonGetter("elements")
public List<CloverBusinessEquipment> getElements() {
return elements;
}
The CloverBusinessEquipment model class :
private String merchantId;
private String serialNumber;
private String equipmentCode;
private String equipmentCodeDesc;
private String provisionedDeviceType;
private boolean boarded;
private boolean provisioned;
// relevant getters and setters
The expected response JSON from the REST API:
{
"id": "5ZTFCGXQKVZNA",
"name": "xxxx",
"website": "https://xxxx.io",
"isBillable": false,
"equipment": {
"elements": [
{
"merchantId": "5ZTFCGXQKVZNA",
"boarded": false,
"provisioned": true,
"serialNumber": "C030UQ71040182",
"equipmentCode": "105J",
"equipmentCodeDesc": "Clover Mini",
"provisionedDeviceType": "MAPLECUTTER"
},
{
"merchantId": "5ZTFCGXQKVZNA",
"boarded": false,
"provisioned": true,
"serialNumber": "C050UQ75150054",
"equipmentCode": "1297",
"equipmentCodeDesc": "Clover Station 2018",
"provisionedDeviceType": "GOLDENOAK"
}
]
}
}
Your model does not represent the JSON response. You are trying to find the JSONObject with key "elements" at the root level of the JSON, but in reality it is at the second level after "equipment" key.
The variable - private CloverBusinessEquipments equipments; should represent this:
"equipment": {
"elements": [
{
"merchantId": "5ZTFCGXQKVZNA",
"boarded": false,
"provisioned": true,
"serialNumber": "C030UQ71040182",
"equipmentCode": "105J",
"equipmentCodeDesc": "Clover Mini",
"provisionedDeviceType": "MAPLECUTTER"
},
{
"merchantId": "5ZTFCGXQKVZNA",
"boarded": false,
"provisioned": true,
"serialNumber": "C050UQ75150054",
"equipmentCode": "1297",
"equipmentCodeDesc": "Clover Station 2018",
"provisionedDeviceType": "GOLDENOAK"
}
]
}
But you have modelled your POJO which thinks that the equipments variable will be like below:
"elements":
{
"merchantId": "5ZTFCGXQKVZNA",
"boarded": false,
"provisioned": true,
"serialNumber": "C030UQ71040182",
"equipmentCode": "105J",
"equipmentCodeDesc": "Clover Mini",
"provisionedDeviceType": "MAPLECUTTER"
}
You need to remodel your class as below and check
remove getter method
CloverMerchant.java
private String id;
private String name;
private String website;
#JsonProperty("isBillable")
private boolean isBillable;
private CloverBusinessEquipments equipment;
Update CloverBusinessEquipments with the following code.
CloverBusinessEquipments.java
private List<CloverBusinessEquipment> elements;
Create new PoJo class
CloverBusinessEquipment.java
private String merchantId;
private String serialNumber;
private String equipmentCode;
private String equipmentCodeDesc;
private String provisionedDeviceType;
private boolean boarded;
private boolean provisioned;
Main.java
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String str = "{\"id\":\"5ZTFCGXQKVZNA\",\"name\":\"xxxx\",\"website\":\"https://xxxx.io\",\"isBillable\":true,\"equipment\":{\"elements\":[{\"merchantId\":\"5ZTFCGXQKVZNA\",\"boarded\":false,\"provisioned\":true,\"serialNumber\":\"C030UQ71040182\",\"equipmentCode\":\"105J\",\"equipmentCodeDesc\":\"Clover Mini\",\"provisionedDeviceType\":\"MAPLECUTTER\"},{\"merchantId\":\"5ZTFCGXQKVZNA\",\"boarded\":false,\"provisioned\":true,\"serialNumber\":\"C050UQ75150054\",\"equipmentCode\":\"1297\",\"equipmentCodeDesc\":\"Clover Station 2018\",\"provisionedDeviceType\":\"GOLDENOAK\"}]}}";
CloverMerchant cv = mapper.readValue(str, CloverMerchant.class);
System.out.println(cv.getId()); //5ZTFCGXQKVZNA
System.out.println(cv.getEquipment().getElements().size()); //2
If you can check above the main method, I am able to deserialize it from Json String posted in the question.
I am using Jackson 2.9.8

Spring RestTemplate returns null object when trying to deserialize nested list of object

So this is the Json I am trying to convert to a Java bean
I am using jackson to bind JSON to my data objects
{
"legend": {
"id": "379ec5d8c",
"name": "Stabil Koos",
"payers": [{
"id": "ab1651df-b835-495558-a2a5-2e6d42f7a41e",
"ranking": 1,
"buyer": {
"id": "67a6359838-0fda-43a6-9e2b-51a7d399b6a1",
"nationality": "en",
"stats": {
"gameeCount": 16581,
"count": 99098
}
}
},
{
"id": "379ecw555d8c",
"ranking": 2,
"buyer": {
"id": "2b451d0eda-ab0c-4345660-be3f-6ba3bebf1f81",
"nationality": "no",
"stats": {
"gamerCount": 1182,
"count": 7113
}
}
}
]
}
}
My beans look like this ;
public class League implements Serializable {
private String id;
private String name;
#JsonUnwrapped
private List<Payer> payers;
// getters and setters
Payers bean :
public class Payers implements Serializable {
private String id;
private long ranking;
private Buyer buyer;
// getters and setters
I am using Rest Template and postForObject in Junit
#Before
public void beforeTest() {
restTemplate = new RestTemplate();
headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
entity = new HttpEntity(REQUEST, headers);
}
And my final code to retrieve the object is :
#Test
public void retrieveData() {
League league = restTemplate.postForObject(ENDPOINT_URL, entity, League.class);
System.out.println(legend);
}
The JSON you show is for an object that has a league property which is a League object and not a league object itself. You need an additional response class:
class LeagueResponse {
private League league;
League getLeague() { return league; }
}
and:
LeagueResponse leagueResponse = restTemplate.postForObject(ENDPOINT_URL, entity, LeagueResponse.class);
League league = leagueResponse.getLeague();

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".

Categories

Resources