我有一个spring项目,其中在控制器中具有以下API隐含内容:
public ResponseEntity<Boolean> testApi(
@ApiParam @RequestParam(value = "xxx", required = false) String name) {
boolean abc = false; // or true
return new ResponseEntity<>(abc, HttpStatus.OK);
}
通过邮递员调用时,API返回的结果很好。但是,在单元测试中,我将其称为:
MappingJackson2HttpMessageConverter converter = new
MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON);
restTemplate.getRestTemplate().getMessageConverters().add(0, converter);
ResponseEntity<Boolean> xyz = restTemplate.getForEntity(url, Boolean.class);
我收到以下错误:
org.springframework.web.client.RestClientException: Error while extracting response for type [class java.lang.Boolean] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.Boolean` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.Boolean` out of START_OBJECT token
注意-我的restTemplate是org.springframework.boot.test.web.client.TestRestTemplate;
并自动连线为:
@Autowired
private TestRestTemplate restTemplate;
仅当调用API返回布尔值时,我才看到此问题。对于其他返回对象的API,效果很好