[英]is there any way to define httpheaders in single class,interface or annotation in java?
is there any way to define httpheaders in single class in java? 有什么办法可以在Java中的单个类中定义httpheaders? It is something like creating a wrapper class for userdefined httpHeaders used in my application. 这类似于为我的应用程序中使用的用户定义的httpHeaders创建包装器类。 instead of defining all headers in my controller method. 而不是在我的控制器方法中定义所有标头。
@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
}
So that It can be interface or annotations. 这样它可以是接口或注释。 Like: 喜欢:
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)
}
So that we can use it as : 这样我们就可以将其用作:
@PostMapping
public <ResponseEntity<Book>> getBooks(
@ModelAttribute("requestContext") RequestContext context, @PathVariable("bookId") String bookId, @MyApplicationHeaders appHeaders{
---implementation
}
According to the docs of @RequestHeader
根据@RequestHeader
的文档
If the method parameter is
Map<String, String>
,MultiValueMap<String, String>
, orHttpHeaders
then the map is populated with all header names and values. 如果方法参数是Map<String, String>
,MultiValueMap<String, String>
或HttpHeaders
则将使用所有标头名称和值填充映射。
This means you can use either a Map
or HttpHeaders
as a parameter type to hold multiple header values. 这意味着您可以使用Map
或HttpHeaders
作为参数类型来保存多个标头值。 In your case, Map
is applicable since you are working on non-standard headers: 在您的情况下, 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.