problem REST response with Spring ResponseEntity<Integer> with JDK 11

sgrillon :

I do migration JDK8 to JDK11 so I update springboot to 2.2.4.RELEASE + add :

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>

I compile in java 8.

Controler:

    @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));
    }

My JSON curl:

$ curl -s --header "Accept: application/json" http://localhost:8084/noraui/api/hello/nbLines

8

My XML curl:

$ curl -s --header "Accept: application/xml" http://localhost:8084/noraui/api/hello/nbLines

<Integer>8</Integer>

My unit test (OK with JDK8 but not with 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);
}

I try with:

@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);
}

My error is:

[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]
sgrillon :

Accept header is now mandatory:

"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);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=19403&siteId=1