java - 为什么迁移到 Java 11 后, header "Accept: application/xml"在 Spring Boot 中不起作用?

标签 java spring spring-boot jackson java-11

我想将我的 Spring-Boot 项目从 JDK 8 迁移到 11。

Controller :

    @RequestMapping(value = "/{model}/columns", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE, produces = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE })
    public ResponseEntity<DataModel> getColumnsModel(@PathVariable String model) {
        LOGGER.info("getColumnsModel : model[{}]", model);
        DataModel dataModel = modelService.getColumns(model);
        return Optional.ofNullable(dataModel).map(result -> new ResponseEntity<>(result, HttpStatus.OK)).orElse(new ResponseEntity<>(HttpStatus.NO_CONTENT));
    }

我的 JSONcurl(在 JDK 8 和 JDK 11 中正常):

curl -s --header "Accept: application/json" http://localhost:8084/noraui/api/hello/columns > target/actual_hello_columns.json

我的 XMLcurl(在 JDK 8 中正常,但在 JDK 11 中不正常):

curl -s --header "Accept: application/xml" http://localhost:8084/noraui/api/hello/columns > target/actual_hello_columns.xml

在 JDK 11 中,结果为空。

如何解决这个问题(我需要在 JDK8 和 JDK11 上使用我的代码)?

编辑:

我有这个错误:

Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]

最佳答案

尝试使用更高版本的 Jackson 来支持 XML。

pom.xml 中添加此依赖项。

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

spring-boot 2.1.6.RELEASE 仅调用 jackson-dataformat-xml 2.9.9,如果您达到 2.10.1 ,它解决了问题。

关于java - 为什么迁移到 Java 11 后, header "Accept: application/xml"在 Spring Boot 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60429282/

相关文章:

java - 使用 selenium webdriver 处理动态表

spring - 路由器功能中的 REST 请求参数验证

spring - FieldPath 字段名不能包含 '.'

amazon-web-services - 如何将 Spring Boot 应用程序中的数据输入 Amazon Kinesis?

Java Spring(页面<实体>.Map(新转换器))

java - Java 中的 T 和继承

java - 在 Java 中创建 alpha 图像

java - Spring boot 安全配置 - 必须指定 authenticationManager

java - 如何构建 Java Spring/Hibernate 后端以实现双向关系

java - 小程序运行时出现代码错误,用户拒绝了权限