我可以提供与 spring-data-rest GET 并行的端点吗?
Can i offer an endpoint in parallel to a spring-data-rest GET?
我的项目正在从自定义 json 格式转向 json-hal 和 spring-data-rest。继续支持 "old" json 我想 运行 现有的资源控制器与新的 Spring-Data-Rest 提供的并行。
每当我将 spring-data-rest 配置为使用与我们现有控制器相同的 url 时,仅使用旧控制器,如果 accept-header 不匹配,我会收到错误响应.当我使用不同的 url 时,一切正常
是否可以 运行 控制器与 spring-data-rest 控制器并行并根据 Accept-Header 响应?
老控制器:
@RepositoryRestController
@RequestMapping(value = "/api/accounts", produces = {"application/custom.account+json"})
public class AccountResource {
@RequestMapping(method = RequestMethod.GET)
@PreAuthorize("#oauth2.hasScope('read') and hasRole('ROLE_ADMIN')")
public ResponseEntity<List<Account>> getAll(
@RequestParam(value = "page", required = false) Integer offset,
@RequestParam(value = "per_page", required = false) Integer limit,
@RequestParam(value = "email", required = false) String email
) throws URISyntaxException {
...
}
}
@RepositoryRestController
在 type level 上与 @RequestMapping
的表现不佳。
第一步,通过从 RequestMapping 中删除 produces
参数(我在这里使用 GetMapping 快捷方式),确保您确实设法捕获了请求。我还删除了 @PreAuthorize 注释,因为它现在不相关,并引入了一个参数来捕获 Accept
header 值(用于调试):
@RepositoryRestController
public class AccountResource {
@GetMapping(value = "/api/accounts")
public ResponseEntity<List<Account>> getAll(
@RequestParam(value = "page", required = false) Integer offset,
@RequestParam(value = "per_page", required = false) Integer limit,
@RequestParam(value = "email", required = false) String email,
) throws URISyntaxException {
...
}
}
有了这个,您应该能够随意自定义 GET /api/accounts 并且仍然受益于 POST/PUT/PATCH... /api/accounts 由 Spring Data Rest 自动提供,并断言 content-type
如果按预期工作,您可以:
- 尝试在 GetMapping 注释中使用
produces = "application/custom.account+json"
(单个值不需要大括号)缩小方法范围,并查看您的端点和 Spring 生成的端点方法都可用
- 恢复您的@PreAuthorize 注释
- 去掉@RequestHeader 参数
这给你:
@RepositoryRestController // NO MAPPING AT THE TYPE LEVEL
public class AccountResource {
@GetMapping(value = "/api/accounts", // Mapping AT THE METHOD LEVEL
produces = "application/custom.account+json") // the content-type this method answers to
@PreAuthorize("#oauth2.hasScope('read') and hasRole('ADMIN')") // ROLE is 'ADMIN' not 'ROLE_ADMIN'
public ResponseEntity<List<Account>> getAll(
@RequestHeader("Content-Type") String contentType,
@RequestParam(value = "page", required = false) Integer offset,
@RequestParam(value = "per_page", required = false) Integer limit,
@RequestParam(value = "email", required = false) String email,
) throws URISyntaxException {
...
}
}
现在:
- curl host:port/api/accounts 将命中 Spring 控制器端点
- curl host:port/api/accounts -H "Accept: application/custom.account+json" 将命中您的自定义控制器端点。
我的项目正在从自定义 json 格式转向 json-hal 和 spring-data-rest。继续支持 "old" json 我想 运行 现有的资源控制器与新的 Spring-Data-Rest 提供的并行。
每当我将 spring-data-rest 配置为使用与我们现有控制器相同的 url 时,仅使用旧控制器,如果 accept-header 不匹配,我会收到错误响应.当我使用不同的 url 时,一切正常
是否可以 运行 控制器与 spring-data-rest 控制器并行并根据 Accept-Header 响应?
老控制器:
@RepositoryRestController
@RequestMapping(value = "/api/accounts", produces = {"application/custom.account+json"})
public class AccountResource {
@RequestMapping(method = RequestMethod.GET)
@PreAuthorize("#oauth2.hasScope('read') and hasRole('ROLE_ADMIN')")
public ResponseEntity<List<Account>> getAll(
@RequestParam(value = "page", required = false) Integer offset,
@RequestParam(value = "per_page", required = false) Integer limit,
@RequestParam(value = "email", required = false) String email
) throws URISyntaxException {
...
}
}
@RepositoryRestController
在 type level 上与 @RequestMapping
的表现不佳。
第一步,通过从 RequestMapping 中删除 produces
参数(我在这里使用 GetMapping 快捷方式),确保您确实设法捕获了请求。我还删除了 @PreAuthorize 注释,因为它现在不相关,并引入了一个参数来捕获 Accept
header 值(用于调试):
@RepositoryRestController
public class AccountResource {
@GetMapping(value = "/api/accounts")
public ResponseEntity<List<Account>> getAll(
@RequestParam(value = "page", required = false) Integer offset,
@RequestParam(value = "per_page", required = false) Integer limit,
@RequestParam(value = "email", required = false) String email,
) throws URISyntaxException {
...
}
}
有了这个,您应该能够随意自定义 GET /api/accounts 并且仍然受益于 POST/PUT/PATCH... /api/accounts 由 Spring Data Rest 自动提供,并断言 content-type
如果按预期工作,您可以:
- 尝试在 GetMapping 注释中使用
produces = "application/custom.account+json"
(单个值不需要大括号)缩小方法范围,并查看您的端点和 Spring 生成的端点方法都可用 - 恢复您的@PreAuthorize 注释
- 去掉@RequestHeader 参数
这给你:
@RepositoryRestController // NO MAPPING AT THE TYPE LEVEL
public class AccountResource {
@GetMapping(value = "/api/accounts", // Mapping AT THE METHOD LEVEL
produces = "application/custom.account+json") // the content-type this method answers to
@PreAuthorize("#oauth2.hasScope('read') and hasRole('ADMIN')") // ROLE is 'ADMIN' not 'ROLE_ADMIN'
public ResponseEntity<List<Account>> getAll(
@RequestHeader("Content-Type") String contentType,
@RequestParam(value = "page", required = false) Integer offset,
@RequestParam(value = "per_page", required = false) Integer limit,
@RequestParam(value = "email", required = false) String email,
) throws URISyntaxException {
...
}
}
现在:
- curl host:port/api/accounts 将命中 Spring 控制器端点
- curl host:port/api/accounts -H "Accept: application/custom.account+json" 将命中您的自定义控制器端点。