I have the following controller (following Rails naming convention)
@RestController("/vehicles")
public class VehiclesController {
@GetMapping
public Iterable<Vehicle> index(
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "25") int per
) {
...
}
@GetMapping("/{id}")
public Vehicle show(@PathVariable long id) {
...
}
@PostMapping
public Vehicle create(@RequestBody Vehicle vehicle) {
...
}
@PatchMapping("/{id}")
public void update(@PathVariable long id, @RequestBody Vehicle params) {
...
}
@DeleteMapping("/{id}")
public void destroy(@PathVariable long id) {
...
}
}
Every time I send the request
GET /vehicles
I expect the request gets routed to the index
method, but it actually is routed to the show
method then failed to serve the content because the framework can't convert "vehicles"
to a long
. Here is the error message:
Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'long'; nested exception is java.lang.NumberFormatException: For input string: "vehicles"
Is there anything wrong in my code? Thanks in advance.
The value
property of @RestController
annotation is not a path prefix but a bean name. To give all annotated methods in your controller a prefix, annotate controller class with @RequestMapping("/vehicles")
.
@RestController
@RequestMapping("/vehicles")
public class VehiclesController {
@GetMapping
public Iterable<Vehicle> index(
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "25") int per
) {
...
}
...
}