web-dev-qa-db-ja.com

Springboot HttpMessageNotWritableException:事前設定されたContent-Type 'null'を持つ[...]のコンバーターなし

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

}

私のコントローラー:

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

私はカールを使用します:

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

私のコンピューター(Windows 10)では、結果は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

nixプレートフォーム(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

Postmanでテストを実行できますか?私はPostmanでテストを行っただけでうまくいきました。ただし、リクエストのヘッダーを2回確認して、ヘッダーが正しいことを確認してください。

PS:私はgithub.com/NoraUi/noraui-datas-webservicesでプロジェクトをチェックし、言及されている依存関係(jaxb-runtimeおよびxerces)を使用してテストを実行しましたが、これらの依存関係はまったく必要ありませんでした。 @XmlRootElementアノテーションで十分です。私はGitHubアカウントに個人プロジェクトを提出しました。わかりやすい例です。ぜひご覧ください: https://github.com/DavidGuimaraes/spring-security-basic

0