Springboot HttpMessageNotWritableException:没有预设 Content-Type 'null' 的 [...] 转换器]

sgr*_*lon 11 java travis-ci spring-boot openjdk-11

我尝试使用 Springboot 2.2.4.RELEASE + JDK11 和 java 8 编译使用 XML 和 JSON 创建 Web API。

我的模型:

@XmlRootElement
public class DataModel {

    private List<String> columns;

    private List<Row> rows;

    public List<String> getColumns() {
        return columns;
    }

    public void setColumns(List<String> columns) {
        this.columns = columns;
    }

    public List<Row> getRows() {
        return rows;
    }

    public void setRows(List<Row> rows) {
        this.rows = rows;
    }

}
Run Code Online (Sandbox Code Playgroud)

我的控制器:

@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));
}
Run Code Online (Sandbox Code Playgroud)

我使用卷曲:

curl -s -v --header "Accept: application/xml" http://localhost:8084/api/foo/columns
Run Code Online (Sandbox Code Playgroud)

在我的电脑(Windows 10)上结果正常。

* TCP_NODELAY set
* Connected to localhost (::1) port 8084 (#0)
> GET /noraui/api/hello/columns HTTP/1.1
> Host: localhost:8084
> User-Agent: curl/7.67.0
> Accept: application/xml
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< Content-Type: application/xml
< Transfer-Encoding: chunked
< Date: Tue, 03 Mar 2020 08:38:20 GMT
<
{ [254 bytes data]
* Connection #0 to host localhost left intact
Run Code Online (Sandbox Code Playgroud)

我在 Unix 平台 (travis-ci) 上的错误:

Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class com.github.noraui.data.rest.DataModel] with preset Content-Type 'null']

< HTTP/1.1 500 
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< Content-Length: 0
< Date: Tue, 03 Mar 2020 08:41:28 GMT
< Connection: close
< 
* Closing connection 0
Run Code Online (Sandbox Code Playgroud)

小智 0

你能用Postman进行测试吗?我刚刚用 Postman 做了测试,效果很好。但是,请检查请求的标头两次并确保其正确。

PS:我在 github.com/NoraUi/noraui-datas-webservices 检查了该项目,并使用提到的依赖项(jaxb-runtime 和 xerces)执行了测试,这些依赖项根本没有必要。@XmlRootElement 注释应该足够了。我刚刚向我的 GitHub 帐户提交了一个个人项目。这是一个更干净的例子。请查看: https: //github.com/DavidGuimaraes/spring-security-basic