Spring RequestMapping用于生成和使用JSON的控制器 [英] Spring RequestMapping for controllers that produce and consume JSON

查看:225
本文介绍了Spring RequestMapping用于生成和使用JSON的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用多个Spring控制器消耗并生成 application / json ,我的代码中充满了长注释,如:



< pre class =lang-java prettyprint-override> @RequestMapping(value =/ foo,method = RequestMethod.POST,
consumemes = MediaType.APPLICATION_JSON_VALUE,
produce = MediaType.APPLICATION_JSON_VALUE)

有没有办法用<生成复合/继承/聚合注释? 的strong>默认值生成,这样我就可以改写:

  @JSONRequestMapping(value =/ foo,method = RequestMethod.POST)

我们如何在上面定义类似 @JSONRequestMapping 的内容?注意传入的方法就像在 @RequestMapping ,如果默认值不合适,也可以传入消耗生成



我需要控制我要归还的东西。我希望生成 / 使用 annotation-methods,以便获得相应的 Content-Type headers。

解决方案

从Spring 4.2.x开始,您可以使用<创建自定义映射注释code> @RequestMapping 作为元注释。所以:


有没有办法生成复合/继承/聚合
注释,其默认值为消耗和产生,这样I
可以代之以:

  @JSONRequestMapping(value =/ foo,method = RequestMethod.POST)


是的,有这样的方法。您可以创建如下的元注释:

  @Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@RequestMapping(consume =application / json,produce =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 =consume)
String [] consumemes()default {};

@AliasFor(annotation = RequestMapping.class,attribute =produce)
String [] produce()default {};
}

然后您可以使用默认设置,甚至可以根据需要覆盖它们:

  @JsonRequestMapping(method = POST)
public String defaultSettings(){
returnDefault settings;
}

@JsonRequestMapping(value =/ override,method = PUT,produce =text / plain)
public String overrideSome(@RequestBody String json){
返回json;
}

您可以阅读更多关于 AliasFor 在spring的 javadoc github wiki


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)

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)

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.

I need to control what I'm returning. I want the produces/consumes annotation-methods so that I get the appropriate Content-Type headers.

解决方案

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;
}

You can read more about AliasFor in spring's javadoc and github wiki.

这篇关于Spring RequestMapping用于生成和使用JSON的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆