我想将我的 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/