Spring MVC 路由到错误的操作
Spring MVC routes to wrong action
我有以下控制器(遵循 Rails 命名约定)
@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) {
...
}
}
每次发送请求
GET /vehicles
我希望请求被路由到 index
方法,但它实际上被路由到 show
方法然后无法提供内容,因为框架无法转换 "vehicles"
到 long
。这是错误消息:
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"
我的代码有什么问题吗?提前致谢。
这是不正确的。
@RestController("/vehicles")
值(“/vehicles”)是组件的名称
为了添加 REST 路径,您需要 @RestController 注释和 class 上的 @RequestMapping 注释。
@RestController()
@RequestMapping(value = "/vehicles")
值 /vehicles 是您的 HTTP 调用的后缀。
您还必须更改 index 方法上的注释:
@RequestMapping(value = "", method = RequestMethod.GET)
public Iterable<Vehicle> index(
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "25") int per
)
一切都应该按预期工作。
@RestController
注解的value
属性不是路径前缀而是bean名。要给控制器中所有带注释的方法一个前缀,请使用 @RequestMapping("/vehicles")
注释控制器 class。
@RestController
@RequestMapping("/vehicles")
public class VehiclesController {
@GetMapping
public Iterable<Vehicle> index(
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "25") int per
) {
...
}
...
}
我有以下控制器(遵循 Rails 命名约定)
@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) {
...
}
}
每次发送请求
GET /vehicles
我希望请求被路由到 index
方法,但它实际上被路由到 show
方法然后无法提供内容,因为框架无法转换 "vehicles"
到 long
。这是错误消息:
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"
我的代码有什么问题吗?提前致谢。
这是不正确的。
@RestController("/vehicles")
值(“/vehicles”)是组件的名称
为了添加 REST 路径,您需要 @RestController 注释和 class 上的 @RequestMapping 注释。
@RestController()
@RequestMapping(value = "/vehicles")
值 /vehicles 是您的 HTTP 调用的后缀。
您还必须更改 index 方法上的注释:
@RequestMapping(value = "", method = RequestMethod.GET)
public Iterable<Vehicle> index(
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "25") int per
)
一切都应该按预期工作。
@RestController
注解的value
属性不是路径前缀而是bean名。要给控制器中所有带注释的方法一个前缀,请使用 @RequestMapping("/vehicles")
注释控制器 class。
@RestController
@RequestMapping("/vehicles")
public class VehiclesController {
@GetMapping
public Iterable<Vehicle> index(
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "25") int per
) {
...
}
...
}