Controller Level Request Mapping with Path Variable
Hi, I am having difficulties to use a controller level request mapping with path variable. It seems to be supported by linkTo(Controller.class, Object... args) which is part of 0.3.0, but the resource is not found.
If I have a controller
@Controller
@RequestMapping("/people/{personId}/products")
public class PersonProductController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public HttpEntity<List<ProductResource>> getProductsOfPerson(@PathVariable Long personId) {
...
}
}
and I link to the controller using linkTo with args like this:
Link personProducts = linkTo(PersonProductController.class, 1L).withRel("personProducts");
then the response creates this link which seems to be correct, but it gives me a 404.
{
"rel":"personProducts",
"href":"http://localhost:8080/people/1/products"
},
Is it possible that in Spring you cannot use path variables on the controller level? Or did I get the usage wrong?
Best regards, Dietrich
did you try to configure the name of the path variable? @PathVariable("personId")
i know from experience that path variables on class level work.
The problem is that you need to annotate the controller with @RestController and not @Controller. Alternatively, annotate the method with @ResponseBody if you leave the class as @Controller.
Hi Dietrich,
Have you been able to get passed this issue? I'm having the same problem trying to pass variables at the controller level Request Mapping and it looks like none of the suggestions work for me. I'm using spring 4.1.1.RELEASE.
Regards, Hooman
Check this one out: https://jira.spring.io/browse/SPR-8858
The proper way to do this today is as follows:
@RestController
@RequestMapping("/people/{employeeId}/products")
static class EmployeeController {
@GetMapping
public EntityModel<Employee> products(@PathVariable Integer employeeId) {
return EntityModel.of(EMPLOYEES.get(employeeId),
linkTo(methodOn(EmployeeController.class).products(employeeId)).withSelfRel());
}
}
And it works properly.
@RestController(or a@Controller+@ReponseBodycombination) denotes JSON not a template to be rendered.- You can apply
@RequestMappingat the controller level to denote a top level. - It's become idiomatic to use
@GetMapping, etc. instead of another@RequestMappingat the method level.
Full test case verifying proper behavior shown below:
@ExtendWith(SpringExtension.class)
@WebAppConfiguration
@ContextConfiguration
class ScratchTest {
@Autowired WebApplicationContext context;
MockMvc mockMvc;
private static Map<Integer, Employee> EMPLOYEES;
@BeforeEach
void setUp() {
this.mockMvc = webAppContextSetup(this.context).build();
EMPLOYEES = new TreeMap<>();
EMPLOYEES.put(0, new Employee("Frodo Baggins", "ring bearer"));
EMPLOYEES.put(1, new Employee("Bilbo Baggins", "burglar"));
}
@Test
void simple() throws Exception {
this.mockMvc.perform(get("/people/0/products/foo")) //
.andDo(print()) //
.andExpect(status().isOk());
}
@RestController
@RequestMapping("/people/{employeeId}/products")
static class EmployeeController {
@GetMapping("/foo")
public EntityModel<Employee> products(@PathVariable Integer employeeId) {
return EntityModel.of(EMPLOYEES.get(employeeId),
linkTo(methodOn(EmployeeController.class).products(employeeId)).withSelfRel());
}
}
@Configuration
@EnableWebMvc
@EnableHypermediaSupport(type = HypermediaType.HAL)
static class TestConfig {
@Bean
EmployeeController employeeController() {
return new EmployeeController();
}
}
}