java - Spring Boot Rest根据参数调用
问题描述
我对以下内容有疑问。我们想创建一些 Rest 调用。其余调用的参数之一是返回格式。所以参数 return_format 可以有 json 或 xml 的值。有没有一种聪明的方法来使用参数并使用能够产生正确输出格式的服务?
如果调用参数return_format == json 那么
@Produces({"application/json"})
如果调用参数 return_format == xml 那么
@Produces({"application/xml"})
解决方案
您不需要像 return_format 这样的参数。这可以通过 Accept 标头来控制。
在控制器中,您可以添加两种格式:
@RequestMapping(value = "/employee", method = RequestMethod.GET,
produces = { "application/json", "application/xml" })
public <yourResponse> get(@RequestHeader("Accept") String accept) {
// Check if Accept is XML or JSON
}
推荐阅读
- gitlab - 如何将本地主分支推送到远程主分支?
- python - 如何使用 PyInstaller 将 python 脚本转换为二进制可执行文件
- neo4j - 为什么一个适度的 MATCH 和 CREATE 查询会在 Neo4j 浏览器中旋转?
- angular - 如何将数组作为数据从角度发送到快递路由器?
- pycaret - PyCaret predict_model() 方法失败并出现 Pipeline not Found 错误
- python - 如果下一个文件已经完成,则 luigi 任务失败
- c++ - 关于return语句出现异常的问题
- asp.net-core - 从 api 向 ms 团队 bot 发送消息
- javascript - 哪些 HTML 元素意味着分词?
- hyperledger-fabric - hyperledger fabric2.2中数据库为leveldb时没有创建容器