本文介绍了Spring RequestMapping用于生成和使用JSON的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 使用多个Spring控制器消耗并生成 application / json ,我的代码中充满了长注释,如: @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开始,您可以使用 @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的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-07 05:22