用于生成和使用 JSON 的控制器的 Spring RequestMapping [英] Spring RequestMapping for controllers that produce and consume JSON
问题描述
使用多个使用和生成 application/json
的 Spring 控制器,我的代码散落着长注释,例如:
With multiple Spring controllers that consume and produce application/json
, my code is littered with long annotations like:
@RequestMapping(value = "/foo", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
有没有办法用consumes
和produces
的default 值生成复合/继承/聚合"注释,这样我可以改为写如下:
Is there a way to produce a "composite/inherited/aggregated" annotation with default values for consumes
and produces
, such that I could instead write something like:
@JSONRequestMapping(value = "/foo", method = RequestMethod.POST)
我们如何定义上面的 @JSONRequestMapping
之类的东西?注意value
和method
和@RequestMapping
一样传入,也好能传入consumes
或 produces
如果默认值不合适.
How do we define something like @JSONRequestMapping
above? Notice the value
and method
passed in just like in @RequestMapping
, also good to be able to pass in consumes
or produces
if the default isn't suitable.
我需要控制返回的内容.我想要 produces
/consumes
注释方法,以便获得适当的 Content-Type
标头.
I need to control what I'm returning. I want the produces
/consumes
annotation-methods so that I get the appropriate Content-Type
headers.
推荐答案
从 Spring 4.2.x 开始,您可以创建自定义映射注释,使用 @RequestMapping
作为元注释.所以:
As of Spring 4.2.x, you can create custom mapping annotations, using @RequestMapping
as a meta-annotation. So:
有没有办法产生复合/继承/聚合"消费和生产的默认值的注释,这样我可以改为写如下:
Is there a way to produce a "composite/inherited/aggregated" annotation with default values for consumes and produces, such that I could instead write something like:
@JSONRequestMapping(value = "/foo", method = RequestMethod.POST)
是的,有这样的方法.您可以创建如下元注释:
Yes, there is such a way. You can create a meta annotation like following:
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@RequestMapping(consumes = "application/json", produces = "application/json")
public @interface JsonRequestMapping {
@AliasFor(annotation = RequestMapping.class, attribute = "value")
String[] value() default {};
@AliasFor(annotation = RequestMapping.class, attribute = "method")
RequestMethod[] method() default {};
@AliasFor(annotation = RequestMapping.class, attribute = "params")
String[] params() default {};
@AliasFor(annotation = RequestMapping.class, attribute = "headers")
String[] headers() default {};
@AliasFor(annotation = RequestMapping.class, attribute = "consumes")
String[] consumes() default {};
@AliasFor(annotation = RequestMapping.class, attribute = "produces")
String[] produces() default {};
}
然后您可以使用默认设置,甚至可以根据需要覆盖它们:
Then you can use the default settings or even override them as you want:
@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 维基.
You can read more about AliasFor
in spring's javadoc and github wiki.
这篇关于用于生成和使用 JSON 的控制器的 Spring RequestMapping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!