6

I've a RestController for a CRUD annotated like this:

@RestController
@RequestMapping("/rest/v1/area")
public class AreaController

My methods are annotated like:

@RequestMapping(value = "/{uuid}", method = RequestMethod.PUT)
public ResponseEntity<Area> save(@Valid @RequestBody Area area) {

Is there a way to use a value for a method to have an absolute path? I want to add a method to upload files, and don't want to make a new Controller just for this. I want a method like:

@RequestMapping(value = "/rest/v1/area-upload", method = RequestMethod.PUT
public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {

Thanks!

Germán
  • 328
  • 6
  • 15
  • I don't think there is some bypass mecanism, the quick and easy way is to replace the type annotation with method annotation for existing methods, then add you new method (note that you can keep the common url part on the type level if you want) –  Aug 04 '16 at 14:35

2 Answers2

12

From the documentation: (spring 2.5 api document of requestmapping)

Method-level mappings are only allowed to narrow the mapping expressed at the class level (if any)

So you cannot override, only narrow it down to sub-paths.

Ofcourse if you create a new controller class and put the method there, you can have it point to whatever path you like.

Joeblade
  • 1,735
  • 14
  • 22
  • Thank you @Joeblade for pointing me in the right direction. I've checked my version: http://docs.spring.io/spring-framework/docs/4.3.2.RELEASE/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html and this still applies. Thank you everyone for the workarounds, I was checking if this was possible without changing all the other mappings. – Germán Aug 04 '16 at 14:53
2

You can just change route mapping for the controller.

@RestController
@RequestMapping("/rest/v1")
public class AreaController

and the mapping for method will be:

@RequestMapping(value = "/area-upload", method = RequestMethod.PUT
public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
Foredoomed
  • 2,219
  • 2
  • 21
  • 39
  • That would change the prefix for all other endpoints within the controller. The assumption here is that you have an endpoint or two which have a different prefix than the rest. – Josh M. May 14 '18 at 13:00