添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
风流的冰淇淋  ·  C#学习笔记 - 知乎·  1 年前    · 
朝气蓬勃的木瓜  ·  metabase ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
  • How to map a list of JSON objects using Spring RestTemplate.
  • How to map nested JSON objects.
  • I am trying to consume https://bitpay.com/api/rates , by following the tutorial from http://spring.io/guides/gs/consuming-rest/ .

    Consider see this answer, specially if you want use generics list stackoverflow.com/questions/36915823/… Moesio Nov 20, 2018 at 18:15

    First define an object to hold the entity coming back in the array.. e.g.

    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Rate {
        private String name;
        private String code;
        private Double rate;
        // add getters and setters
    

    Then you can consume the service and get a strongly typed list via:

    ResponseEntity<List<Rate>> rateResponse =
            restTemplate.exchange("https://bitpay.com/api/rates",
                        HttpMethod.GET, null, new ParameterizedTypeReference<List<Rate>>() {
    List<Rate> rates = rateResponse.getBody();
    

    The other solutions above will also work, but I like getting a strongly typed list back instead of an Object[].

    This run's smoothly with Spring 4.2.3 and - as Matt said - has the big advantage of avoiding the Object[] – Marged Dec 16, 2015 at 12:21 @Matt - which marshaller are you using to marshal the json into Rate objects? I am guessing that's what's happening here, at the time of the restTemplate.exchange a marshallar maps all the json values to the matching key names as properties in the Rate object. Hope my thought process is correct. – Nirmal Mar 28, 2016 at 20:14 @SarvarNishonboev the current ParameterizedTypeReference from springframework.core still seems fine: docs.spring.io/spring-framework/docs/current/javadoc-api/org/… – fspinnenhirn Oct 26, 2019 at 0:24

    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().

    That worked like a charm , thank you . Maybe you can direct me to some other tutorials or guides that I could read on this topic ? – Karudi May 15, 2014 at 11:18 best to look here on stackoverflow for some code snippets and examples or visit the offial spring website...... TblGps[] a = responseEntity.getBody(); – kamokaze May 15, 2014 at 11:29 Is it possible to this using generics? i.e. my method has a Class<T extends Foo> parameter and I would like to get a collection of T from the getForEntity method. – Diskutant Mar 20, 2015 at 12:52 Yes it should work, but might not be out of the box depending on your spring/jackson version and your class types. Its all about serializing/deserializing generics - the http Request istself does not care what is transported. – kamokaze Mar 21, 2015 at 10:42
    Object[] forNow = template.getForObject("URL", Object[].class);
        searchList= Arrays.asList(forNow);
    

    Where Object is the class you want

    This works even if you use a class and not Object like Coupon[] coupons = restTemplate.getForObject( url, Coupon[].class) – lrkwz Dec 2, 2015 at 16:06 This can cause NPE if HTTP response body was empty (not [] but totally empty). So be careful and check for null (if (forNow != null)...). – Ruslan Stelmachenko Dec 10, 2017 at 3:55 Saved my ass :) Wondering what type is used by Jackson, when Object.class is specified in method getForObject(). – Eric May 28, 2019 at 7:09

    then using ParameterizedTypeReference of List of BitPay you can use as:

    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<List<Employee>> response = restTemplate.exchange(
      "https://bitpay.com/api/rates",
      HttpMethod.GET,
      null,
      new ParameterizedTypeReference<List<BitPay>>(){});
    List<Employee> employees = response.getBody();
    

    If you would prefer a List of POJOs, one way to do it is like this:

    class SomeObject {
        private int id;
        private String name;
    
    public <T> List<T> getApi(final String path, final HttpMethod method) {     
        final RestTemplate restTemplate = new RestTemplate();
        final ResponseEntity<List<T>> response = restTemplate.exchange(
          path,
          method,
          null,
          new ParameterizedTypeReference<List<T>>(){});
        List<T> list = response.getBody();
        return list;
    

    And use it like so:

     List<SomeObject> list = someService.getApi("http://localhost:8080/some/api",HttpMethod.GET);
    

    Explanation for the above can be found here (https://www.baeldung.com/spring-rest-template-list) and is paraphrased below.

    There are a couple of things happening in the code above. First, we use ResponseEntity as our return type, using it to wrap the list of objects we really want. Second, we are calling RestTemplate.exchange() instead of getForObject().

    This is the most generic way to use RestTemplate. It requires us to specify the HTTP method, optional request body, and a response type. In this case, we use an anonymous subclass of ParameterizedTypeReference for the response type.

    This last part is what allows us to convert the JSON response into a list of objects that are the appropriate type. When we create an anonymous subclass of ParameterizedTypeReference, it uses reflection to capture information about the class type we want to convert our response to.

    It holds on to this information using Java’s Type object, and we no longer have to worry about type erasure.

    Another way in kotlin is to do: val rates = restTemplate.exchange("https://bitpay.com/api/rates", HttpMethod.GET, null, Array<Rate>::class.java).body!! – DagR Feb 22 at 8:56

    After multiple tests, this is the best way I found :)

    Set<User> test = httpService.get(url).toResponseSet(User[].class);
    

    All you need there

    public <T> Set<T> toResponseSet(Class<T[]> setType) {
        HttpEntity<?> body = new HttpEntity<>(objectBody, headers);
        ResponseEntity<T[]> response = template.exchange(url, method, body, setType);
        return Sets.newHashSet(response.getBody());
    

    3 alternative ways have mentioned here to retrieve list of objects. All of these will work flawlessly

    @RequestMapping(value = "/emp2", produces = "application/json")
    public List<Employee> getEmp2()
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        HttpEntity<String> entity = new HttpEntity<String>(headers);
        ResponseEntity<List<Employee>> response = restTemplate.exchange(
                "http://hello-server/rest/employees", HttpMethod.GET,entity, 
        new ParameterizedTypeReference<List<Employee>>() {});
        return response.getBody();
    
    @RequestMapping(value = "/emp3", produces = "application/json")
    public List<Employee> getEmp3()
        Employee[] empArray = restTemplate.getForObject("http://hello-server/rest/employees", Employee[].class);
        List<Employee> emp= Arrays.asList(empArray);
        return emp;
    
        @RequestMapping(value = "/emp4", produces = "application/json")
    public Employee[] getEmp4()
        ResponseEntity<Employee[]> responseEntity = restTemplate.getForEntity("http://hello-server/rest/employees", Employee[].class);
        Employee[] empList = responseEntity.getBody();
        //MediaType contentType = responseEntity.getHeaders().getContentType();
        //HttpStatus statusCode = responseEntity.getStatusCode();
        return  empList;
    

    Employee.class

    public class Employee {
    private Integer id;
    private String name;
    private String Designation;
    private String company;
    //getter setters and toString()
    

    i actually deveopped something functional for one of my projects before and here is the code :

    * @param url is the URI address of the WebService * @param parameterObject the object where all parameters are passed. * @param returnType the return type you are expecting. Exemple : someClass.class public static <T> T getObject(String url, Object parameterObject, Class<T> returnType) { try { ResponseEntity<T> res; ObjectMapper mapper = new ObjectMapper(); RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8"))); ((SimpleClientHttpRequestFactory) restTemplate.getRequestFactory()).setConnectTimeout(2000); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<T> entity = new HttpEntity<T>((T) parameterObject, headers); String json = mapper.writeValueAsString(restTemplate.exchange(url, org.springframework.http.HttpMethod.POST, entity, returnType).getBody()); return new Gson().fromJson(json, returnType); } catch (Exception e) { e.printStackTrace(); return null; * @param url is the URI address of the WebService * @param parameterObject the object where all parameters are passed. * @param returnType the type of the returned object. Must be an array. Exemple : someClass[].class public static <T> List<T> getListOfObjects(String url, Object parameterObject, Class<T[]> returnType) { try { ObjectMapper mapper = new ObjectMapper(); RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8"))); ((SimpleClientHttpRequestFactory) restTemplate.getRequestFactory()).setConnectTimeout(2000); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<T> entity = new HttpEntity<T>((T) parameterObject, headers); ResponseEntity<Object[]> results = restTemplate.exchange(url, org.springframework.http.HttpMethod.POST, entity, Object[].class); String json = mapper.writeValueAsString(results.getBody()); T[] arr = new Gson().fromJson(json, returnType); return Arrays.asList(arr); } catch (Exception e) { e.printStackTrace(); return null;

    I hope that this will help somebody !

    In my case I preferred to extract a String then browse the context using JsonNode interface

        var response =  restTemplate.exchange("https://my-url", HttpMethod.GET, entity,  String.class);
        if (response.getStatusCode() == HttpStatus.OK) {
            var jsonString = response.getBody();
            ObjectMapper mapper = new ObjectMapper();
            JsonNode actualObj = mapper.readTree(jsonString);           
            System.out.println(actualObj);  
    

    or quickly

    ObjectNode actualObj= restTemplate.getForObject("https://my-url", ObjectNode.class);
    

    then read inner data with path expression i.e.

    boolean b = actualObj.at("/0/states/0/no_data").asBoolean();
    

    An Easier Way:
    I will show you guys both with Authorization heard and without Authorization header:

  • Without Authorization:
    a. Do Dependency Injection(Constructor Injection): You can also prefer field injection. I considered constructor injection.
  • public class RestTemplateService {
    private final RestTemplate template;
    public RestTemplateService(RestTemplate template) {
        this.template = template;
    

    b. Invoke getList() method:

    public ResponseEntity<List> getResponseList(String url, HttpMethod type) {
        return template.exchange(url, type, new HttpEntity<>(new HttpHeaders()), List.class);
    
  • With Authorization: I love small methods. So I segregated the functionalities as such:
  •  public ResponseEntity<List> getResponse(String url, HttpMethod type) {
        return template.exchange(url, type, getRequest(getHeaders(USERNAME, PASS)), List.class);
     private HttpEntity<String> getRequest(HttpHeaders headers) {
        return new HttpEntity<>(headers);
     private HttpHeaders getHeaders(String username, String password) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Basic " + new String(Base64.encodeBase64((username + ":" + password).getBytes())));
        return headers;
    


    Hopefully the problem will be resolved!

    I found work around from this post https://jira.spring.io/browse/SPR-8263.

    Based on this post you can return a typed list like this:

    ResponseEntity<? extends ArrayList<User>> responseEntity = restTemplate.getForEntity(restEndPointUrl, (Class<? extends ArrayList<User>>)ArrayList.class, userId);
                    This won't work, because due to erasure no type parameter information is passed to getForEntity. Also (Class<? extends ArrayList<User>>) ArrayList.class gives an "incompatible types" compile error.
    – Esko Luontola
                    Oct 5, 2015 at 15:15