我有两个问题:
如何使用Spring RestTemplate映射JSON对象列表。 如何映射嵌套的JSON对象。
我试图消费https://bitpay.com/api/rates,从http://spring.io/guides/gs/consuming-rest/遵循教程。
我有两个问题:
如何使用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;
}
希望问题能得到解决!
其他回答
我在这里遇到的最大问题是构建将RestTemplate匹配到兼容类所需的Object结构。幸运的是,我找到了http://www.jsonschema2pojo.org/(在浏览器中获得JSON响应并将其作为输入),我非常推荐这个功能!
也许这样……
ResponseEntity<Object[]> responseEntity = restTemplate.getForEntity(urlGETList, Object[].class);
Object[] objects = responseEntity.getBody();
MediaType contentType = responseEntity.getHeaders().getContentType();
HttpStatus statusCode = responseEntity.getStatusCode();
RequestMapping的控制器代码
@RequestMapping(value="/Object/getList/", method=RequestMethod.GET)
public @ResponseBody List<Object> findAllObjects() {
List<Object> objects = new ArrayList<Object>();
return objects;
}
ResponseEntity是HttpEntity的扩展,它添加了HttpStatus状态码。在RestTemplate和@Controller方法中使用。 在RestTemplate中,该类由getForEntity()和exchange()返回。
更简单的方法: 我会给你们看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;
}
希望问题能得到解决!
在我的情况下,我更喜欢提取一个字符串,然后使用JsonNode接口浏览上下文
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);
}
或者很快
ObjectNode actualObj= restTemplate.getForObject("https://my-url", ObjectNode.class);
然后读取内部数据与路径表达式,即。
boolean b = actualObj.at("/0/states/0/no_data").asBoolean();
这里提到了3种检索对象列表的方法。所有这些都将完美地工作
@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()
}