首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用JDK 11实现SpringResponseEntity<Integer>的问题REST响应

用JDK 11实现SpringResponseEntity<Integer>的问题REST响应
EN

Stack Overflow用户
提问于 2020-02-27 22:03:00
回答 2查看 779关注 0票数 2

我将JDK8迁移到JDK11,因此我将springboot更新为2.2.4.RELEASE + add:

代码语言:javascript
复制
<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>

我用java 8编译。

控制器:

代码语言:javascript
复制
    @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

代码语言:javascript
复制
8

我的XML卷曲:

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

代码语言:javascript
复制
<Integer>8</Integer>

我的单元测试( JDK8 确定,JDK11没有):

代码语言:javascript
复制
@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);
}

我试着:

代码语言:javascript
复制
@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);
}

我的错误是:

代码语言:javascript
复制
[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]
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-28 08:14:04

Accept报头现在是强制

"Accept: application/json

代码语言:javascript
复制
@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);
}
票数 1
EN

Stack Overflow用户

发布于 2020-02-27 22:59:35

尝试将-Djava.locale.providers=COMPAT添加为VM选项,并查看它是否通过。

您还可以打印实体体来查看实际接收到的内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60442378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档