我将JDK8迁移到JDK11,因此我将springboot更新为2.2.4.RELEASE + add:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>我用java 8编译。
控制器:
@RequestMapping(value = "/{model}/nbLines", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE, produces = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<Integer> getNbLines(@PathVariable String model) {
LOGGER.info("getNbLines : model[{}]", model);
Integer nbLines = modelService.getNbLines(model);
return Optional.ofNullable(nbLines).map(result -> new ResponseEntity<>(result, HttpStatus.OK)).orElse(new ResponseEntity<>(HttpStatus.NO_CONTENT));
}我的JSON卷发
$ curl -s --标题"Accept: application/json“http://localhost:8084/noraui/api/hello/nbLines
8我的XML卷曲:
$ curl -s --标题"Accept: application/xml“http://localhost:8084/noraui/api/hello/nbLines
<Integer>8</Integer>我的单元测试( JDK8 确定,JDK11没有):
@Test
public void getHelloNbLines() {
ResponseEntity<Integer> entity = new RestTemplate().getForEntity("http://localhost:" + port + "/noraui/api/hello/nbLines", Integer.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo(8);
}我试着:
@Test
public void getHelloNbLines() {
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
ResponseEntity<Integer> entity = new RestTemplate().exchange("http://localhost:" + port + "/noraui/api/hello/nbLines", HttpMethod.GET, new HttpEntity<Object>(headers), Integer.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo(8);
}我的错误是:
[ERROR] getHelloNbLines(com.github.noraui.datas.web.services.rest.NoraUiDatasControllerTests) Time elapsed: 0.135 s <<< FAILURE!
org.springframework.web.client.RestClientException:
Error while extracting response for type [class java.lang.Integer] and content type [application/xml]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.Integer` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.Integer` out of START_OBJECT token
at [Source: (PushbackInputStream); line: 1, column: 1]发布于 2020-02-28 08:14:04
Accept报头现在是强制
"Accept: application/json
@Test
public void getHelloNbLines() {
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Accept", "application/json");
ResponseEntity<Integer> entity = new RestTemplate().exchange("http://localhost:" + port + "/noraui/api/hello/nbLines", HttpMethod.GET, new HttpEntity<Object>(headers), Integer.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo(8);
}发布于 2020-02-27 22:59:35
尝试将-Djava.locale.providers=COMPAT添加为VM选项,并查看它是否通过。
您还可以打印实体体来查看实际接收到的内容。
https://stackoverflow.com/questions/60442378
复制相似问题