使用多个消费和生产 application/json
的 Spring 控制器,我的代码充满了长注释,例如:
@RequestMapping(value = "/foo", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
有没有办法使用 consumes
和 produces
的 默认 值生成“复合/继承/聚合”注释,这样我就可以改写如下内容:
@JSONRequestMapping(value = "/foo", method = RequestMethod.POST)
我们如何定义类似 @JSONRequestMapping
的东西? Notice the value
and method
passed in just like in @RequestMapping
, also good to be able to pass in consumes
or produces
如果默认值不合适。
我需要控制我要返回的内容。我想要 produces
/ consumes
注释方法,以便获得适当的 Content-Type
标头。
原文由 Roshan Mathews 发布,翻译遵循 CC BY-SA 4.0 许可协议
从 Spring 4.2.x 开始,您可以创建自定义映射注释,使用
@RequestMapping
作为元注释。所以:}
@JsonRequestMapping(method = POST) public String defaultSettings() { return “Default settings”; }
@JsonRequestMapping(value = “/override”, method = PUT, produces = “text/plain”) public String overrideSome(@RequestBody String json) { return json; }
”`
你可以在 spring 的 javadoc 和 github wiki 中阅读更多关于
AliasFor
的信息。