我设计了一个API,我有两种选择可以添加标头参数: 在bean config中声明:
@Bean
public Docket api() {
//Adding Header
ParameterBuilder headerParameterBuilder = new ParameterBuilder();
java.util.List<Parameter> headersParameters = new ArrayList<Parameter>();
headersParameters.clear();
headerParameterBuilder.name(Headers.TRACE_ID).description("ID of the whole trace forest").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
headersParameters.add(headerParameterBuilder.build());
return new Docket(DocumentationType.SWAGGER_2).useDefaultResponseMessages(false).select()
.apis(RequestHandlerSelectors.basePackage(swaggerResourcePackage)).paths(PathSelectors.any()).build()
.apiInfo(new ApiInfo(swaggerTitle, swaggerDescription, swaggerVersion, "", null, "", "", Collections.emptyList()))
.protocols( new HashSet<>(Arrays.asList("http","https")))
.globalOperationParameters(headersParameters);
}
或作为参数传递:
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Search Corporate information found", response = ResultSearch.class) })
@GetMapping(path = "corporates", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ApiOperation(value = "Search for corporates")
public Object searchCorporate( @RequestHeader(value="TRACE_ID") String tracer) throws Exception {
//TODO
}
据我了解,第一个仅用于结构化Swagger API,第二个仅用于结构化和标头验证。 我的问题是,如果我有通用的班级标题,该怎么设计?经过必要的验证!