简体   繁体   English

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

[英]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> , or HttpHeaders 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. 这意味着您可以使用MapHttpHeaders作为参数类型来保存多个标头值。 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.

相关问题 如何在Java中定义一个接口方法来接受一个类或其任何子类? - How to define an interface method to accept a class or any of its subclasses in Java? Java-强制实现接口的类声明注释 - Java - Force a class implementing an interface to declare an annotation 有没有一种方法可以使用Java的Override注释指定要覆盖的接口/类? - Is there a way to specify the interface/class being overrode using Java's Override annotation? 定义接口和类的最佳/标准方法 - best/standard way to define interface and class 有没有办法在Java中混合或继承注释值? - Is there any way to mix or inherit annotation value in Java? 在类中实现时,是否有任何方法可以在Interface中生成代码 - Is there any way to generate code in Interface on implementing it in a class Java接口中的注释 - Annotation in Java Interface 正则表达式以匹配Java类/接口/枚举/注释定义 - regular expression to match java class/interface/enum/annotation definition 有什么方法可以在Java中定义一个私有类,只有同一包中的其他类才能访问它? - Is there any way to define a private class in Java which only other classes in the same package can access it? 有没有办法不用Java实现接口方法? - Is there any way not to implement interface methods in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM