spring - 根据@RequestMapping 的产生条件返回错误响应的内容类型
问题描述
我的 REST Web 服务中有自定义错误处理。我有返回 XML / JSON 作为响应的方法。在 SpringBoot 版本2.0.9上一切正常。但是在迁移到最新版本(2.2.4)之后,我的错误处理测试失败了:
Content type expected:<application/xml> but was:<application/json>
Expected :application/xml
Actual :application/json
经过研究,我发现它与将 Spring 升级到 5.1 版本有关。文件:
错误响应的内容协商 @RequestMapping 的产生条件不再影响错误响应的内容类型。
如何在最新的 Spring 早期版本中重现行为?我只想返回产生条件中指定的错误的内容类型。
休息方法:
@PostMapping(path = "/{scriptName}", produces = { MediaType.APPLICATION_XML_VALUE })
public ResponseEntity<Object> xmlMethod(@RequestParam("payload") String payload, @PathVariable("scriptName") String scriptName) {
Object result = service.call(payload, scriptName);
return ResponseEntity.ok(new JsonBuilder(result).getContent());
}
@PostMapping(path = "/{scriptName}", produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<Object> jsonMethod(@RequestParam("payload") String payload, @PathVariable("scriptName") String scriptName) {
Object result = service.call(payload, scriptName);
return ResponseEntity.ok(new JsonBuilder(result).getContent());
}
CustomRestExceptionHandler:
@ControllerAdvice
public class CustomRestExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<Object> handleResourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
HTTPErrorDTO httpError = new HTTPErrorDTO(HttpStatus.NOT_FOUND, ex.getLocalizedMessage());
return new ResponseEntity<>(httpError, new HttpHeaders(), httpError.getStatus());
}
....
// handlers for other exceptions
}
错误 DTO:
@XmlRootElement(name = "Exception")
@XmlAccessorType(XmlAccessType.FIELD)
public class HTTPErrorDTO {
private HttpStatus status;
private String message;
private List<String> errors;
}
相关主题:Spring mvc - 为 XML 和 JSON 响应配置错误处理
- -编辑
我尝试添加自定义内容协商配置。但是我的 REST API 的一个客户端向我发送 content-type = "application/x-www-form-urlencoded"并期望 application/xml 并且不发送任何接受标头。
所以我只能在方法/控制器级别决定内容类型应该是什么格式。
我可以以某种方式从控制器通知异常处理程序,应该设置哪一种内容类型?
解决方案
在您的CustomRestExceptionHandler
中,您可以为ResponseEntity
.
return ResponseEntity.status(httpError.getStatus().value())
.contentType(MediaType.APPLICATION_XML)
.body(httpError);
推荐阅读
- java - 可以在不影响性能的情况下将 Integer.MAX_VALUE 传递给 elasticsearch 吗?
- xml - XSLT 1.0 去除空白
- java - 使用 jdk 8 的默认 jaxws-ri 实现禁用soap客户端的soap验证
- woocommerce - 用户未登录时如何使用 wp cli 清除 woocommerce 购物车?
- scala - 为Scala中的多个变量分配相同的值
- jquery - 如果我在选择中更改选项,则在模板中出现错误消息后 Django 选择选项休息
- html - 从网页中提取 html 行并粘贴到 Excel 中
- javascript - Sailsjs 从两个不同的 mysql 数据库中获取数据
- excel - 如何使用 VBA 将 Powerquery 代码打印到 Excel?
- c# - 类项目为什么需要在aspnetcore2.2中添加『Microsoft.AspNetCore.MVC』 ProjectReference 如果已经包含了『Microsoft.AspNetCore.App 2.2.0』