[英]How to define an interface method to accept a class or any of its subclasses in Java?
[英]is there any way to define httpheaders in single class,interface or annotation in java?
有什么办法可以在Java中的单个类中定义httpheaders? 这类似于为我的应用程序中使用的用户定义的httpHeaders创建包装器类。 而不是在我的控制器方法中定义所有标头。
@PostMapping
public <ResponseEntity<Book>> getBooks(
@ModelAttribute("requestContext") RequestContext context, @PathVariable("bookId") String bookId,
@RequestHeader(value="x-institute-id") String instituteId,
@RequestHeader(value="x-customer-last-logged-time") String xCustomerLastLoggedTime,
@RequestHeader(value="x-customer-ip-address") String xCustomerIpAddress,
@RequestHeader(value="x-interaction-id", required=false) String xInteractionId){
---implementation
}
这样它可以是接口或注释。 喜欢:
public class MyApplicationHeaders{
@RequestHeader(value="x-institute-id") String instituteId,
@RequestHeader(value="x-customer-last-logged-time") String xCustomerLastLoggedTime,
@RequestHeader(value="x-customer-ip-address") String xCustomerIpAddress,
@RequestHeader(value="x-interaction-id", required=false) String xInteractionId)
}
这样我们就可以将其用作:
@PostMapping
public <ResponseEntity<Book>> getBooks(
@ModelAttribute("requestContext") RequestContext context, @PathVariable("bookId") String bookId, @MyApplicationHeaders appHeaders{
---implementation
}
根据@RequestHeader
的文档
如果方法参数是
Map<String, String>
,MultiValueMap<String, String>
或HttpHeaders
则将使用所有标头名称和值填充映射。
这意味着您可以使用Map
或HttpHeaders
作为参数类型来保存多个标头值。 在您的情况下, Map
适用,因为您正在处理非标准标头:
@PostMapping
public ResponseEntity<Book> getBooks(
@ModelAttribute("requestContext") RequestContext requestContext,
@PathVariable("bookId") String bookId,
@HttpHeaders Map<String, String> appHeaders) {
String instituteId = appHeaders.get("x-institute-id");
// do something else
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.