Add a header parameter in Swagger UI documentation with Springfox

后端 未结 3 1304
梦毁少年i
梦毁少年i 2020-12-19 02:26

I want to add a header parameter field in the auto-generated swagger ui documentation of my rest service. I use Spring and Springfox.

public ResponseEntity&l         


        
3条回答
  •  别那么骄傲
    2020-12-19 03:04

    If you are having more header parameters, then every API will have that many @RequestHeader

    To avoid this and your API looks simple you can use HeaderInterceptor to capture the header information.

    In preHandle() ,  you need to extract the headerInfo in to a an Object and set it as RequestAttribute
    
      public class MyHeaderInterceptor extends HandlerInterceptorAdapter {
    
      @Override
      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception { 
    
        HeaderVo headerVo = HeaderVo.createReqHeaderinput(
                request.getHeader("authorization"),
                request.getHeader("contentType"),                
                request.getHeader("myHeaderParam0"),
                request.getHeader("myHeaderParam1"), 
                request.getHeader("myHeaderParam3"),
                request.getHeader("myHeaderParam4"),
                request.getHeader("myHeaderParam5")
    
                );
    
         // You can do any validation of any headerInfo here.
         validateHeader(headerVo);
    
         request.setAttribute("headerName", headerVo);
         return true;
       }
    
     }
    

    Your API will looks like the below with a @RequestAttribute("headerName")

    public @ResponseBody
    ResponseEntity getSomeApi(
            //Headers common for all the API's       
    
            @RequestAttribute("headerName") HeaderVo header ,
            @ApiParam(value = "otherAPiParam", required = true, defaultValue = "") 
            @PathVariable(value = "otherAPiParam") String otherAPiParam,
            @ApiParam(value = "otherAPiParam1", required = true, defaultValue = "") 
            @RequestParam(value = "otherAPiParam1") String otherAPiParam1,
            @ApiParam(value = "otherAPiParam2, required = true, defaultValue = "")
            @RequestParam(value = "otherAPiParam2") String otherAPiParam2
         ) throws MyExcp  {
      ....
     }
    

    Your Swagger still should describes all headers of the API, for that you can add parameters in swagger Docket, SwaggerConfig Please note ignoredParameterTypes, we mentioned to ignore HeaderVo, because that is internal to the application. swagger doesnt require to show that

    @Bean
    public Docket postsApi() {
    
        //Adding Header
        ParameterBuilder aParameterBuilder = new ParameterBuilder();
        List aParameters = new ArrayList();
    
        aParameters.clear();
    
        aParameterBuilder.name("myHeaderParam0").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        aParameters.add(aParameterBuilder.build());
        aParameterBuilder.name("myHeaderParam1").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        aParameters.add(aParameterBuilder.build());
       ....
       ....
    
        return new Docket(DocumentationType.SWAGGER_2).groupName("public-api")
                .apiInfo(apiInfo()).select().paths(postPaths()).build().ignoredParameterTypes(HeaderVo.class).globalOperationParameters(aParameters);
    
       }
    

提交回复
热议问题