我有两个问题:

如何使用Spring RestTemplate映射JSON对象列表。 如何映射嵌套的JSON对象。

我试图消费https://bitpay.com/api/rates,从http://spring.io/guides/gs/consuming-rest/遵循教程。


当前回答

更简单的方法: 我会给你们看Authorization heard和unauthorization header:

未经授权: a.进行依赖注入(构造函数注入): 你也可以选择字段注入。我考虑了构造函数注入。

public class RestTemplateService {
private final RestTemplate template;
public RestTemplateService(RestTemplate template) {
    this.template = template;
  }
}

b.调用getList()方法:

public ResponseEntity<List> getResponseList(String url, HttpMethod type) {
    return template.exchange(url, type, new HttpEntity<>(new HttpHeaders()), List.class);
  }

授权:我喜欢小方法。所以我把这些功能分开:

 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;
  }

希望问题能得到解决!

其他回答

经过多次测试,这是我发现的最好的方法:)

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

这就是你所需要的

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());
}

首先定义一个对象来保存返回数组的实体。如。

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

然后你可以使用该服务并通过以下方式获得一个强类型列表:

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

上面的其他解决方案也可以工作,但我喜欢得到一个强类型列表,而不是Object[]。

对于那些使用Spring + Kotlin的人来说,下面是翻译:

val rates = restTemplate.exchange("https://bitpay.com/api/rates", HttpMethod.GET, null, object : ParameterizedTypeReference<List<Rate>>() {}).body!!

更简单的方法: 我会给你们看Authorization heard和unauthorization header:

未经授权: a.进行依赖注入(构造函数注入): 你也可以选择字段注入。我考虑了构造函数注入。

public class RestTemplateService {
private final RestTemplate template;
public RestTemplateService(RestTemplate template) {
    this.template = template;
  }
}

b.调用getList()方法:

public ResponseEntity<List> getResponseList(String url, HttpMethod type) {
    return template.exchange(url, type, new HttpEntity<>(new HttpHeaders()), List.class);
  }

授权:我喜欢小方法。所以我把这些功能分开:

 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;
  }

希望问题能得到解决!

实际上,我之前为我的一个项目开发了一些功能,以下是代码:

/**
 * @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;
    }
}

我希望这能帮助到别人!