Springboot HttpMessageNotWritableException: No converter for […] with preset Content-Type 'null']

一个人想着一个人 提交于 2021-01-28 23:27:01

问题


I try create web API with XML and JSON with Springboot 2.2.4.RELEASE + JDK11 and java 8 compilation.

my model:

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

}

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

I use a curl:

curl -s -v --header "Accept: application/xml" http://localhost:8084/api/foo/columns

On my computer (windows 10) the result is OK.

* 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

My error on Unix plateform (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

回答1:


Can you perform a test with Postman? I just did a test with Postman and worked fine. However, check the header of your request twice and make sure that it is right.

P.S.: I checked the project at github.com/NoraUi/noraui-datas-webservices and performed tests with the dependencies mentioned (jaxb-runtime and xerces) and those dependencies were not necessary at all. The @XmlRootElement annotation should be enough. I just submitted a personal project to my GitHub account. It is a cleaner example. Please, check it out: https://github.com/DavidGuimaraes/spring-security-basic




回答2:


I had a similar error but with JSON. Although this POST is very old, I would like to share my experience just in case it helps down the track.

My problem happened because the POJO that I tried to convert to JSON had to properties with the same name

@JsonProperty("my_property")
[...]
@JsonProperty("my_property")  // duplicated property name in the same POJO.

After fixing the duplication,the object could be converted correctly.




回答3:


I add this dependency

<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
</dependency>


来源:https://stackoverflow.com/questions/60502811/springboot-httpmessagenotwritableexception-no-converter-for-with-preset-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!