繁体   English   中英

有什么方法可以在Java中的单个类,接口或注释中定义httpheaders?

[英]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则将使用所有标头名称和值填充映射。

这意味着您可以使用MapHttpHeaders作为参数类型来保存多个标头值。 在您的情况下, 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM