【发布时间】:2020-01-11 22:28:42
【问题描述】:
我有 API:
@GetMapping(path = "/users/{userId}")
public ResponseEntity<UserDTO> getUserById(@PathVariable(value = "userId") Long userId) {
//logic here
}
它应该返回 JSON 响应。
还有一个我无权访问的应用程序,它调用我的 API,例如,GET /users/123.xml 以接收 XML 响应。
但在这种情况下,我的 API 失败并出现 400 错误,因为它无法将 123.xml 解析为 Long。
选项@GetMapping(value = {"/users/{userId}", "/users/{userId}.xml"}) 失败并出现同样的错误。
在调用/{userId}.xml 时如何使用XML 语法响应,同时在调用/{userId} 时使用JSON 语法响应?
编辑:
我希望它无需专门添加“接受”标头,也无需编写任何其他逻辑,它将解析 {userId}.xml,然后设置适当的响应类型。
【问题讨论】:
-
你用的是哪个版本的spring boot?
-
我用
Spring-Boot 2.1.3.RELEASE -
获取 userId 参数作为字符串并使用例如解析它正则表达式检测 xml 后缀
标签: java xml spring-boot path-variables