我尝试用XML创建web,用Springboot 2.2.4.RELEASE + JDK11和Java8编译创建JSON。
my模型:
@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)上,结果是可以的。
* 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我在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发布于 2021-06-01 15:33:25
我通过添加以下依赖项解决了这个问题:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.12.3</version>
</dependency>发布于 2020-08-03 01:33:32
我也犯了一个类似的错误,但是使用了JSON。
我的问题之所以发生,是因为我试图转换为JSON的POJO必须具有相同的名称
@JsonProperty("my_property")
[...]
@JsonProperty("my_property") // duplicated property name in the same POJO.在修复复制后,可以正确地转换对象。
发布于 2022-01-08 16:40:46
在我的例子中,我正在开发一个现有的api,该api生成一个PDF,必须将响应更改为JSON。我忘了从produces = "application/pdf"注释中删除@GetMapping。
https://stackoverflow.com/questions/60502811
复制相似问题