is there any way to define httpheaders in single class in java? It is something like creating a wrapper class for userdefined httpHeaders used in my application. 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
If the method parameter is
Map<String, String>
,MultiValueMap<String, String>
, orHttpHeaders
then the map is populated with all header names and values.
This means you can use either a Map
or HttpHeaders
as a parameter type to hold multiple header values. In your case, Map
is applicable since you are working on non-standard headers:
@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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.