简体   繁体   English

检查 controller 是否动态具有@RequestHeader(在测试中)

[英]Check if a controller has a @RequestHeader dynamically (in a test)

Using SpringBoot I managed to get the list of all controllers dynamically (in a test) using RequestMappingHandlerMapping , but I cannot check if the controller uses the @RequestHeader("language") or not.使用 SpringBoot,我设法使用RequestMappingHandlerMapping动态(在测试中)获取所有控制器的列表,但我无法检查 controller 是否使用 @RequestHeader("language") 。 Is there a way to retrieve this information?有没有办法检索这些信息? I don't think it's possible from RequestMappingHandlerMapping .我认为RequestMappingHandlerMapping不可能。

Thanks.谢谢。

public void randomApi(@PathVariable("user") String user,
                      @RequestHeader("language") String language){...}

RequestMappingHandlerMapping represents all controller methods, you have to get a particular controller method that you are interested from it first. RequestMappingHandlerMapping代表所有 controller 方法,您必须首先从中获取您感兴趣的特定 controller 方法。 The easiest way to do it is first give a name to the controller method such as GetRandomApi :最简单的方法是首先为 controller 方法命名,例如GetRandomApi

@GetMapping(name = "GetRandomApi" , value= "/random")
public void randomApi(@PathVariable("user") String user, @RequestHeader("language") String language){

}

and then get the controller method by this name:然后通过此名称获取 controller 方法:

HandlerMethod hm = mapping.getHandlerMethodsForMappingName("GetRandomApi").get(0);

Please note HandlerMethod represents a controller method and I assume you only has one controller method with this name.请注意HandlerMethod代表 controller 方法,我假设您只有一个具有此名称的 controller 方法。

To check if this controller method has a parameter which is annotated with @RequestHeader , you can do something likes:要检查此 controller 方法是否具有使用@RequestHeader注释的参数,您可以执行以下操作:

for( MethodParameter param : hm.getMethodParameters()){     
        RequestHeader requestHeader = param.getParameterAnnotation(RequestHeader.class);
        if(requestHeader != null) {
            System.out.println(String.format("parameter index %s is annotated with @RequestHeader with the value %s", 
            param.getParameterIndex(), 
            requestHeader.value()));
        }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单元测试 - 使用 MockMvc 使用 @RequestHeader 和 HashMap 测试 controller - Unit Test - Using MockMvc to test the controller with @RequestHeader with HashMap 如何验证RestHeader控制器端点的RequestHeader - How To Validate RequestHeader For RestAPI Controller Endpoint Junit测试Spring MVC 4控制器的案例,以检查@RequestMapping中的@PathVariable参数 - Junit Test cases for Spring MVC 4 controller to check @PathVariable parameters in @RequestMapping 如何对具有对象的RequestParam的Controller方法进行单元测试? - How to unit test a Controller method that has a RequestParam of an object? 如何检查Arquillian测试中已调用该方法 - How to check that method has been invoked in arquillian test 单元测试:检查是否已调用特定的可比对象 - Unit Test : check if a particular Comparable has been called 如何在具有工厂方法的spring MVC控制器中进行集成测试? - How to do integrating test in a spring MVC controller which has factory method? 如何实际测试具有空指针异常的控制器的服务器响应 - How can I realistically test the server response of a controller that has a null pointer exception 如何为具有 Post 方法的控制器类编写单元测试? - How do I write a unit test for a controller class that has a Post method? 如何使用 Postman 测试控制器的方法,该方法在 Spring 应用程序中将一个(或多个)对象作为参数? - How do I test with Postman a Controller's method which has one (or multiple) objects as parameters in a Spring Application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM