Get List of JSON Objects with Spring Resttemplate

Get list of JSON objects with Spring RestTemplate

Maybe this way...

ResponseEntity<Object[]> responseEntity = restTemplate.getForEntity(urlGETList, Object[].class);
Object[] objects = responseEntity.getBody();
MediaType contentType = responseEntity.getHeaders().getContentType();
HttpStatus statusCode = responseEntity.getStatusCode();

Controller code for the RequestMapping

@RequestMapping(value="/Object/getList/", method=RequestMethod.GET)
public @ResponseBody List<Object> findAllObjects() {

List<Object> objects = new ArrayList<Object>();
return objects;
}

ResponseEntity is an extension of HttpEntity that adds a HttpStatus status code. Used in RestTemplate as well @Controller methods.
In RestTemplate this class is returned by getForEntity() and exchange().

How to get List from Object in Spring RestTemplate

Try this out. This should work.

ResponseEntity<String[]> responseEntity = restTemplate.getForEntity("localhost:8083/connectors/", String[].class);
List<String> object = Arrays.asList(responseEntity.getBody());

For simple cases the code above works, but when you have complex json structures which you want to map, then it is ideal to use ParameterizedTypeReference.

ResponseEntity<List<String>> responseEntity =
restTemplate.exchange("localhost:8083/connectors/",
HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {
});
List<String> listOfString = responseEntity.getBody();

How do I get a JSON object with Spring RestTemplate?

Since you are using Spring Boot, it usually comes bundled with handy tools for JSON parsing.
Spring Boot wires per default jackson into your application.

The first thing, you'll need is a (reduced) POJO model of the response.

@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponsePojo {
@JsonProperty("<jsonFieldName>") // only required, if fieldName != jsonFieldName
private List<String> residents;

/* getter & setter ommitted */
}

In your calling code, use something like

ResponsePojo response = restTemplate.getForObject(url, ResponsePojo.class);
response.getResidents() gives you access to the contents of 'resident' array

What happens behind the scenes ?

RestTemplate sends your request and tries to parse the response into your ResponsePojo object.
Since the pojo is a reduced representation of the response, we provided the annotation @JsonIgnoreProperties(ignoreUnknown = true).
This tells the parser, that it should simple ignore any field in the json, which cannot be mapped to your pojo. Since a field is provided, with the exact name like in json, the parser is able to identify and map them accordingly.

How to get the JSONArray List in RestTemplate Spring

You need to modify your Demo class like this

public class Demo {

String initial;

List<Object> answer;

@JsonProperty("final")
String finalValue;
}

To match both the json's.

  1. answer is a array, so you need to declare it as a list.
  2. you cannot use final as field name, because final is a keyword. Alternatively, you can create some other field like finalValue, but map the json final to this field using @JsonProperty

Spring restTemplate get raw json string

You don't even need ResponseEntitys! Just use getForObject with a String.class like:

final RestTemplate restTemplate = new RestTemplate();
final String response = restTemplate.getForObject("https://httpbin.org/ip", String.class);

System.out.println(response);

It will print something like:

{
"origin": "1.2.3.4"
}

Java Spring RestTemplate get 1 object from api

Since you are using Java 16, you have to export your DTO package inside your module setup in order for the jackson object mapper inside the rest template to have access to your DTO class and its fields for reflection

Module-info.java:

module com.windsoftware.kassa.windkassadesktop {
requires javafx.controls;
requires javafx.fxml;
requires javafx.web;

requires org.controlsfx.controls;
requires com.dlsc.formsfx;
requires validatorfx;
requires org.kordamp.ikonli.javafx;
requires org.kordamp.bootstrapfx.core;
requires eu.hansolo.tilesfx;
requires spring.boot;
requires spring.web;
requires com.fasterxml.jackson.databind;
requires com.fasterxml.jackson.annotation;
requires spring.boot.autoconfigure;

opens link.to.class to javafx.fxml;
exports link.to.class;
exports link.to.class.controllers;
exports link.to.class.DTO;
opens link.to.class.controllers to javafx.fxml;
}

How do I unmarshal a dynamic type from JSON with Spring RestTemplate and Jackson

You can use @JsonTypeInfo and @JsonSubType annotations to marshal the given input.

On your dynamicAttributes field you can add a JsonTypeInfo annotation to specify how to create a dynamicAttribute's object. The EXTERNAL_PROPERTY type conveys that the type field is present at the level of dynamicAttributes.

  @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property ="type", include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
visible = true)
DynamicAttributes dynamicAttributes;

To specify the possible subtypes use the JsonSubTypes annotation as shown below

@JsonSubTypes({
@JsonSubTypes.Type(value = DynamicAttributesType1.class, name = "type1")
, @JsonSubTypes.Type(value = DynamicAttributesType2.class, name = "type2")
})
public abstract class DynamicAttributes{
}


Related Topics



Leave a reply



Submit