使用REST客户端从JSON获取值
我的api工作正常,并且在POSTMAN中测试时确实返回了JSON结果。我有问题通过使用REST客户端的控制器检索JSON值。我正在使用spring和hibernate。使用REST客户端从JSON获取值
@RequestMapping(value = "/viewForm/{id}") public String viewForm(@Valid @PathVariable("id") String id, Model model) {
RestTemplate restTemplate = new RestTemplate();
String fooResourceUrl
= "http://localhost:8080/api/delivery/searchDanceform/"+ id;
System.out.println(fooResourceUrl);
ResponseEntity<DanceApplicationForm> rateResponse =
restTemplate.exchange(fooResourceUrl,
HttpMethod.GET, null, DanceApplicationForm.class);
DanceApplicationForm response = rateResponse.getBody();
System.out.println(response);
logger.info("result = {}", response);
return VIEW_PATH + "dance-profile";
}
我得到这个错误,
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/intranet-web] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 404 null] with root cause org.springframework.web.client.HttpClientErrorException: 404 null
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:78)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
at sg.com.ctc.intranet.web.training.controller.DanceController.viewDanceApplicationForm(DanceController.java:239)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
我自己也尝试这种方式。
String response = restTemplate.getForObject(fooResourceUrl, String.class);
但仍然无法正常工作。
回答:
你正在通过null
值交换的方法,实际上代替null
你必须通过实体引用。
HttpHeaders headers = new HttpHeaders(); headers.set("Accept", "application/json");
HttpEntity entity = new HttpEntity(headers);
ResponseEntity<DanceApplicationForm> rateResponse = restTemplate.exchange(url, HttpMethod.GET, entity, DanceApplicationForm.class);
回答:
我把它放在答案部分,因为我不太确定我可以在哪里回复我的代码。
@RequestMapping(value = "/viewDanceApplication/{id}") public String viewDanceApplicationForm(@Valid @PathVariable("id") String id, Model model) {
RestTemplate restTemplate = new RestTemplate();
String fooResourceUrl
= "http://localhost:8080/api/delivery/searchDanceform/"+ id;
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");
HttpEntity entity = new HttpEntity(headers);
ResponseEntity<DanceApplicationForm> rateResponse = restTemplate.exchange(fooResourceUrl, HttpMethod.GET, entity, DanceApplicationForm.class);
logger.info("result = {}", rateResponse);
model.addAttribute("showProfile",rateResponse);
return VIEW_PATH + "dance-profile";
}
回答:
这是
处理程序控制器http://localhost:8080/api/delivery/searchDanceform/ @RequestMapping(value = "/searchDanceform/{id}")
public String searchDanceform(@Valid @PathVariable("id") String id, Model model) {
// do your code and return response
}
以上是 使用REST客户端从JSON获取值 的全部内容, 来源链接: utcz.com/qa/261392.html