1

I'm using @RestController with @RequestMapping annotations to define all of my servlets with spring-mvc.

Question: how can I define some defaults for those annotation, so I don't have to repeat the same configuration regarding eg consumes and produces?

I'd like to always apply the following config, without having to repeat it on each path:

@GetMapping(produces = {APPLICATION_XML_VALUE, APPLICATION_JSON_VALUE})

@PostMapping(
consumes = {APPLICATION_XML_VALUE, APPLICATION_JSON_VALUE},
produces = {APPLICATION_XML_VALUE, APPLICATION_JSON_VALUE})
membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • I haven't done spring-mvc in a long time, but can't you just write your own annotation? – Philipp Sander Jul 20 '17 at 12:52
  • 1
    Unfortunately @GetMapping has `@Target(ElementType.METHOD)`, so I cannot just create my own annotation and reuse it. Neither can I add the annotation to class level of my servlet controller. It's not allowed at class level... – membersound Jul 20 '17 at 12:56
  • 1
    you can apply produces and consumes on RequestMapping on class level. – Sangam Belose Jul 20 '17 at 12:58
  • I don't want to set `consumes` on `@RequestMapping(method = GET)`! Only on the POST mapping. – membersound Jul 20 '17 at 13:00
  • check this https://stackoverflow.com/questions/35123835/spring-requestmapping-for-controllers-that-produce-and-consume-json – Sangam Belose Jul 20 '17 at 13:06
  • @SangamBelose that looks like the solution. Though it's not very nice having to copy the whole `@GetMapping` annotation class content therefore. – membersound Jul 20 '17 at 13:12

3 Answers3

4

Probably it's easiest to just create a custom @RestController annotation and use that on classlevel. Then I only have to repeat the @PostMapping(consumes...) mappings:

@Target(ElementType.TYPE)
@Retention(value=RUNTIME)
@RestController
@RequestMapping(produces = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE})
public @interface DefaultRestController {

}

Usage like:

@DefaultRestController
public class MyServlet {
         @GetMapping("/getmap") //inherits the 'produces' mapping
         public void getmap() {
         }

         @PostMapping("/postmap", consumes = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE})
         public void postmap() {
         }
}

Better than nothing.

membersound
  • 81,582
  • 193
  • 585
  • 1,120
0

The target for RequestMapping annotation could be either a method or class. It can be used instead of GetMapping and PostMapping annotations that target only methods.

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/GetMapping.html

Specifically, @GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/PostMapping.html

Specifically, @PostMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.POST).

Assuming your Controller Name is HelloController, add the RequestMapping annotation with appropriate methods at Class level so that it applies automatically to all the paths.

@Controller
@RequestMapping(method={RequestMethod.GET,RequestMethod.POST}, consumes = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE },produces = { MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE },)
class HelloController{
} 

This confgiuration can be overridden by annotating it in individual methods.

JSN
  • 2,035
  • 13
  • 27
0

You can put an annotation on class. Here is an example:

@RestController
@RequestMapping(
    consumes = {APPLICATION_XML_VALUE, APPLICATION_JSON_VALUE},
    produces = {APPLICATION_XML_VALUE, APPLICATION_JSON_VALUE}
)
public class MyClass {

    // after that you don't have to put any
    // @RequestMapping default values before methods

}
t.piwowarczyk
  • 365
  • 2
  • 5
  • 17
  • Again, as already stated: I do not(!) want to set the `consumes` properties on both get+post (which is the case in your code). – membersound Jul 20 '17 at 20:53