0

I've a controller class with the following signature:

@Controller
@RequestMapping("/home/users")
public class MaterialsController { ... }

and every method in this controller class starts with the same path variable, that is, {username}, for example, I've the following method:

@RequestMapping(value = "{username}/mycreations/{coursewareName}/materials/{materialId}", method = RequestMethod.DELETE)
public void deleteCourseMaterialFromCreatedCourseware(@PathVariable String username,
        @PathVariable String coursewareName, @PathVariable String materialId, Model model,
        HttpServletRequest request, HttpServletResponse response) {

Now, it's a little bit tedious to write {username} at the beginning of each of these controller's methods. Is there a way to specify this path variable at class level? Of course I also need to access it then, like in the same way I'm specifying @PathVariable String username in the method above.

Also, I ideally, I would like also to include at class-level more than one variable, when necessary.

nbro
  • 15,395
  • 32
  • 113
  • 196
  • You can move the path to the class level yes, but not the parameters. – Sotirios Delimanolis May 15 '16 at 17:44
  • 1
    So, for example, I could have at class level the following path `/home/users/{username}`, and then at method level having just `mycreations/{coursewareName}/materials/{materialId}"`, but still keeping the `@PathVariable String username` at method level? – nbro May 15 '16 at 17:49

1 Answers1

1

Yes, you can put it at the class level, but you have to include it in the path variable in the appropriate methods.

Can I use path variable in spring controller class?

Community
  • 1
  • 1
shankarsh15
  • 1,947
  • 1
  • 11
  • 16