I’ve got a Spring @RequestMapping with a couple of @PathVariables, and the first one is necessary to narrow down to the second one – you can see in the example below that I need to get the Department in order to get the Module. Using plain String @PathVariables I can do it like this:
@RequestMapping("/admin/{dept}/{mod}/")
public String showModule(@PathVariable String dept, @PathVariable String mod) {
Department department = dao.findDepartment(dept);
Module module = department.findModule(mod);
return "view";
}
But I’m keen to use Spring’s Converter API to be able to specify the Department directly as the @PathVariable. So this works after I’ve registered a custom Converter class:
@RequestMapping("/admin/{dept}/")
public String showDept(@PathVariable Department dept) {
return "view";
}
But the Converter API doesn’t give access outside of the single argument being converted, so it’s not possible to implement the Converter for Module. Is there another API I can use? I’m eyeing up HandlerMethodArgumentResolver – has anyone solved a problem like this, or are you sticking to String @PathVariables?
I’m using Spring 3.1.
I haven’t done it like this but one way I thought of was to make a separate converter for the both of them:
And have the converter able to take an input of the form “deptid-modid” e.g. “ch-c104”. It wouldn’t be possible to separate them with a slash as the request wouldn’t match the RequestMapping pattern of /admin/*/.
In my case, the requirements have changed slightly so that module codes are fully unique and don’t need to be scoped to department. So I don’t need to do this any more. If I did, I would probably eschew the automatic Module conversion and do it manually in the method.