Git Product home page Git Product logo

Comments (5)

Laures avatar Laures commented on May 28, 2024

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

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

from spring-hateoas.

ccampo133 avatar ccampo133 commented on May 28, 2024

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.

from spring-hateoas.

hoomanb1 avatar hoomanb1 commented on May 28, 2024

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

from spring-hateoas.

nucatus avatar nucatus commented on May 28, 2024

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

from spring-hateoas.

gregturn avatar gregturn commented on May 28, 2024

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();
		}
	}
}

from spring-hateoas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.