json - Spring 请求返回 json 或 xml 响应
问题描述
我有一个 Spring 请求映射,我想默认返回 XML,或者如果在请求标头中指定了 JSON。这是一些代码:
请求映射
@RequestMapping(value = "/batch/{progName}", method = RequestMethod.GET, produces = "application/xml)
public ResponseEntity<JobResults> processTestObject(@PathVariable("progName") String progName,
@RequestHeader("Content-Type") String contentType) throws Exception {
HttpHeaders responseHeaders = MccControllerUtils.createCacheDisabledHeaders();
if(contentType.equals("application/json")) {
responseHeaders.setContentType(MediaType.APPLICATION_JSON);
}
LOGGER.info("Running batch program " + progName);
JobResults response = batchService.processProgName(progName);
return new ResponseEntity<JobResults>(response, responseHeaders, HttpStatus.OK);
}
- 使用没有标题的邮递员,当我点击这个端点时,我会收到一个
400 Bad Request
状态码。 - 在 Postman Headers 字段中,如果我指定
Content-Type
为application/xml
我得到正确的 XML 响应。 - 如果我
Content-Type
在application/json
收到错误消息时指定:Unexpected '<'
我想要的是:
默认情况下从端点返回 XML,如果在请求中指定则返回 JSON
编辑
截至目前,当Postman400 Bad Request
中没有Accept
或发送时,请求返回。Content-Type
为了检索所需的响应,我必须指定Accept
andContent-Type
application/xml
@RequestMapping(value = "/batch/{progName}", method = RequestMethod.GET)
public ResponseEntity<JobResults> processTestObject(@PathVariable("progName") String progName,
@RequestHeader("Content-Type") MediaType contentType) throws Exception {
HttpHeaders responseHeaders = MccControllerUtils.createCacheDisabledHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_XML);
if(!contentType.toString().equals("*/*")) {
responseHeaders.setContentType(contentType);
}
LOGGER.info("Running batch program " + progName);
JobResults response = batchService.processProgName(progName);
return new ResponseEntity<JobResults>(response, responseHeaders, HttpStatus.OK);
}
解决方案
您的示例仍然Content-Type
取自应在的请求Accept
。其次,确保您的 JobResults 对象可被 json marshaller 和 xml marshaller 序列化。第三,如果你想要特定的行为是请求 json,你应该检查那个特定的媒体类型,而不是像"*/*"
.
控制器:
@RequestMapping(value = "/batch/{progName}", method = RequestMethod.GET)
public ResponseEntity<JobResults> processTestObject(@PathVariable("progName") String progName,
@RequestHeader("Accept") MediaType accept) throws Exception {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_XML);
if(!accept.toString().equals("*/*")) {
responseHeaders.setContentType(accept);
}
JobResults response = new JobResults("23423", "result");
return new ResponseEntity<JobResults>(response, responseHeaders, HttpStatus.OK);
}
一些响应对象,但已注释。
@XmlRootElement
public class JobResults {
String id;
String name;
public JobResults(String id, String name) {
this.id = id;
this.name = name;
}
....
}
application/json
当通过标头请求时,上面的代码给了我Accept
。
推荐阅读
- firebase - 如何使用颤振流从 Firestore 获取数据
- c - 混淆比较结构
- java - 在 Spring Boot 中动态加载 Spring Bean 和组件
- amazon-web-services - 将数据从 S3 移动到 Amazon Aurora Postgres
- javascript - 如何在不影响网页响应性的情况下将搜索栏放在导航栏的中心?
- spring-boot - CI中的Spring Boot禁用测试
- r - 根据 gps 坐标列表更改基于开放街道地图的 igraph 中路径边缘的权重
- excel - 使用 Excel 365,如何生成随机项目的随机列表,不重复,同时可能忽略某些行
- android - WifiNetworkSpecifier - 单个网络可用时自动连接
- calculator - 计算器示例/JS 和 HTML 元素之间的链接