Comments (5)
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.
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.
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.
Check this one out: https://jira.spring.io/browse/SPR-8858
from spring-hateoas.
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)
- Upgrade to JSONPath 2.9.0
- Upgrade to JSONPath 2.9.0
- Upgarde to Logback 1.4.14
- Upgrade to Jackson 2.15.4
- Upgrade to Reactor 2022.0.17
- Upgrade to Slf4j 2.0.12
- Upgrade to Mockk 1.13.10
- Release 2.1.4
- Release 2.2.1
- Release 2.3 M1
- Upgrade to Spring Framework 6.1.6
- Upgrade to Reactor 2023.0.5
- Upgrade to Lombok 1.18.32
- Release 2.3 RC1
- Release 2.2.2
- Release 2.1.5
- Upgrade to Spring Framework 6.1.6
- Upgrade to Reactor 2023.0.5
- Upgrade to Spring Framework 6.0.19
- Upgrade to Reactor 2022.0.18
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-hateoas.