1.定義全局的請求參數時,
defaultValue不能是中文,不然一直是請求中
ParameterBuilder userName = new ParameterBuilder(); ParameterBuilder tokenPar1 = new ParameterBuilder(); List<Parameter> pars = new ArrayList<>(); userName.name("userName").description("用戶名").defaultValue("userName").modelRef(new ModelRef("string")) .parameterType("header").required(false).build(); tokenPar1.name("estateId").description("樓盤ID").defaultValue("estateId").modelRef(new ModelRef("string")) .parameterType("header").required(false).build(); pars.add(userName.build()); pars.add(tokenPar1.build()); Docket docket = new Docket(DocumentationType.SWAGGER_2).select() .apis(RequestHandlerSelectors.basePackage("com.zyh.learn.zyhlearn")).paths(PathSelectors.ant("/**")) .build().globalOperationParameters(pars).apiInfo(apiInfo);
2.有時候swagger找不到頁面資源,需要自己定義資源指向
@Configuration public class SwaggerWebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
3.swagger的notes里換行不是\n
swagger-springmvc 換行使用 <br/>
springfox-swagger2 換行使用 空格+空格+\n
4.含有@PathVariable的url傳參
轉載自https://www.cnblogs.com/alsodzy/p/9290836.html
@ApiOperation(value="獲取用戶詳細信息", notes="根據url的id來獲取用戶詳細信息") @ApiImplicitParam(paramType="path", name = "id", value = "用戶ID", required = true, dataType = "Long") @RequestMapping(value="/{id}", method=RequestMethod.GET) public User getUser(@PathVariable Long id) { return users.get(id); }
詳細的注解說明
- @Api:用在類上,說明該類的作用
- @ApiOperation:用在方法上,說明方法的作用
- @ApiImplicitParams:用在方法上包含一組參數說明
- @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個請求參數的各個方面
- paramType:參數放在哪個地方
- header-->請求參數的獲取:@RequestHeader
- query-->請求參數的獲取:@RequestParam
- path(用於restful接口)-->請求參數的獲取:@PathVariable
- body(不常用)
- form(不常用)
- name:參數名
- dataType:參數類型
- required:參數是否必須傳
- value:參數的意思
- defaultValue:參數的默認值
- paramType:參數放在哪個地方
- @ApiResponses:用於表示一組響應
- @ApiResponse:用在@ApiResponses中,一般用於表達一個錯誤的響應信息
- code:數字,例如400
- message:信息,例如"請求參數沒填好"
- response:拋出異常的類
- @ApiModel:描述一個Model的信息(這種一般用在post創建的時候,使用@RequestBody這樣的場景,請求參數無法使用@ApiImplicitParam注解進行描述的時候)
- @ApiModelProperty:描述一個model的屬性