【发布时间】:2017-09-21 11:48:01
【问题描述】:
我有一个 spring boot 应用程序,它有一个 spring MVC 控制器。我正在尝试使用 Accept 标头对我的 rest api 进行版本控制。
以下是我的控制器的样子
RestController
@RequestMapping(value = "/private/")
public class AppleController {
private final AppleService appleService;
public AppleController(AppleService appleService) {
this.appleService = appleService;
}
@GetMapping(value = "apples/{id}", produces = "application/json; v=1.0",
headers = "Accept=application/json; v=1.0")
public ResponseEntity getByappleId(@PathVariable("id") Long appleId) {
System.out.println("version1");
GetByappleIdResponse response = appleService.findByappleId(appleId);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@GetMapping(value = "apples/{id}", produces = "application/json; v=2.0",
headers = "Accept=application/json; v=2.0")
public ResponseEntity getByappleId2(@PathVariable("id") Long appleId) {
System.out.println("version2");
GetByappleIdResponse response = appleService.findByappleId2(appleId);
return new ResponseEntity<>(response, HttpStatus.OK);
}
无论调用 API 时我在 Accept 标头中传递的版本如何,总是调用“getByappleId”方法,因此只返回版本 1 响应。
我的控制器有什么问题吗?
【问题讨论】:
-
似乎Accept标头中的accept-params没有被spring使用。
-
离题不要使用 system.out 尽可能使用记录器
标签: spring-mvc spring-boot spring-restcontroller