在微服务之间互相调用restful api的时候有会遇到406问题,
比如异常 HttpMediaTypeNotAcceptableException 或 HttpClientErrorException:406
使用restTemplate.exchange()或者execute()等方法调用Restful API call 去获取一个json或者xml并解析到pojo的时候:
(下列程序XMLPOJO为一个xml存储对象, 可以是@JacksonXmlRootElement(localName = "xml")的类等)
ResponseEntity<XMLPOJO> response = restTemplate.exchange("https//xxxx.xxxx.xxx/xxx/xx", HttpMethod.POST, entity, XMLPOJO.class);
如果返回406, 或者报异常: org.springframework.web.client.HttpClientErrorException$NOT ACCEPTABLE: 406
说明server返回的值和你需求的值不匹配..
比如我们有server端controller代码:
@RequestMapping(value = "/api1", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<String> chat(@RequestBody String info, final HttpServletRequest request) {
xxxx;
}