I have the following controller:
@RestController
@RequestMapping("/api/{brand}")
public class CarController {
private final CarService carService;
@Autowird
public CarController(CarService carService) {
this.carService = carService;
}
@GetMapping
public Resources<Car> getCars(@PathVariable("brand") String brand) {
return new Resources<>(carService.getCars(brand));
}
@GetMapping(value = "/{model}")
public Car getModel(@PathVariable("brand") String brand, @PathVariable("model") String model) {
return carService.getCar(brand, model);
}
}
I would expect an http GET call to http://localhost:8080/api/bmw
to return me the result of the getCars
method. Instead, the call is delegated to the getModel
method. This returns an error, because there is no {model}
path variable.
How come my http calls are delegated to the incorrect @GetMapping
?
Here you can see the version of spring-boot-starter-web
that I pull in via hateoas
:
[INFO] +- org.springframework.boot:spring-boot-starter-hateoas:jar:2.1.9.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-web:jar:2.1.9.RELEASE:compile
[INFO] | | - org.springframework.boot:spring-boot-starter-tomcat:jar:2.1.9.RELEASE:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.26:compile
[INFO] | | - org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.26:compile
[INFO] | +- org.springframework.hateoas:spring-hateoas:jar:0.25.2.RELEASE:compile
[INFO] | - org.springframework.plugin:spring-plugin-core:jar:1.2.0.RELEASE:compile
I've enabled the mappings
endpoint of Spring Actuator and I can even see that the endpoint that is being ignored is available:
{
"handler": "public org.springframework.hateoas.Resources<com.example.Car> com.example.CarController.getCars(java.lang.String)",
"predicate": "{GET /api/{brand}, produces [application/hal+json]}",
"details": {
"handlerMethod": {
"className": "com.example.CarController",
"name": "getCars",
"descriptor": "(Ljava/lang/String;)Lorg/springframework/hateoas/Resources;"
},
"requestMappingConditions": {
"consumes": [],
"headers": [],
"methods": [
"GET"
],
"params": [],
"patterns": [
"/api/{brand}"
],
"produces": [
{
"mediaType": "application/hal+json",
"negated": false
}
]
}
}
}
EDIT I've added an interceptor that enables me to see what the target handlerMethod
will be.
The handlerMethod
is the correct one:
public org.springframework.hateoas.Resources com.example.CarController.getCars(java.lang.String)
Yet I still get the following error:
Internal server error: Missing URI template variable 'model' for method parameter of type String
I can't wrap my head around the fact that the handlerMethod
does not expect the model
parameter, but spring still throws an error because of it.