spring-hateoas icon indicating copy to clipboard operation
spring-hateoas copied to clipboard

Controller Level Request Mapping with Path Variable

Open dschulten opened this issue 14 years ago • 5 comments

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

dschulten avatar Oct 21 '12 18:10 dschulten

did you try to configure the name of the path variable? @PathVariable("personId")

i know from experience that path variables on class level work.

Laures avatar Jan 03 '13 14:01 Laures

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.

ccampo133 avatar Aug 01 '14 02:08 ccampo133

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

hoomanb1 avatar Nov 15 '14 13:11 hoomanb1

Check this one out: https://jira.spring.io/browse/SPR-8858

nucatus avatar Jul 11 '15 19:07 nucatus

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 + @ReponseBody combination) denotes JSON not a template to be rendered.
  • You can apply @RequestMapping at the controller level to denote a top level.
  • It's become idiomatic to use @GetMapping, etc. instead of another @RequestMapping at 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();
		}
	}
}

gregturn avatar Jun 04 '20 20:06 gregturn