1. 程式人生 > >用swagger生成接口文檔代碼

用swagger生成接口文檔代碼

handler apach urn ica common system tex ons import


1、Swagger2類:
package com.example.demo;
import com.google.common.base.Predicate; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiOperation; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler; import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class Swagger2 { // @ApiOperation(value = "獲取指定id用戶詳細信息", // notes = "根據user的id來獲取用戶詳細信息", // httpMethod = "GET") // @ApiImplicitParams({
// @ApiImplicitParam(name = "userName", value = "用戶名", // paramType = "query", required = true, dataType = "String"), // @ApiImplicitParam(name = "password", value = "用戶密碼", // paramType = "query", required = true, dataType = "String") // }) // @Api //:註解controller,value為@RequestMapping路徑 // // @ApiOperation //:註解方法,value為簡要描述,notes為全面描述,hidden=true Swagger將不顯示該方法,默認為false // // @ApiParam //:註解參數,hidden=true Swagger參數列表將不顯示該參數,name對應參數名,value為註釋,defaultValue設置默認值,allowableValues設置範圍值,required設置參數是否必須,默認為false // // @ApiModel //:註解Model // @ApiModelProperty //:註解Model下的屬性,當前端傳過來的是一個對象時swagger中該對象的屬性註解就是ApiModelProperty中的value @ApiIgnore //:註解類、參數、方法,註解後將不在Swagger UI中顯示 @Bean public Docket createRestApi() { Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() { @Override public boolean apply(RequestHandler input) { if (input.isAnnotatedWith(ApiOperation.class)) { return true; } return false; } }; return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) // document info .select() .apis(predicate) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("buzhou match") .description("buzhou exchange match system") .license("Apache License Version 2.0") .version("1.0") .build(); } }




2、TestController類:

package com.example.demo;

import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
public class TestController {
// private static Logger logger = LogManager.getLogger(TestController.class);


@ApiOperation(value="根路徑", httpMethod="GET", notes="這是一個hello world")
@GetMapping("/")
public String getStr() {
// logger.info("****Hello World!****");
System.out.println("****Hello World!****");
return "結果是:返回helloworld";
}




@ApiOperation(value="異常", httpMethod="GET", notes="這是一個除0的異常")
@GetMapping("/zeroException")
public int ZeroException() {
return 100/0;
}




@ApiOperation(value="獲取年齡", httpMethod="GET", notes="獲取年齡接口")
@GetMapping("/getage")
public int getage(int age) {
return age;
}


private int age;

@ApiOperation(value="設置年齡", httpMethod="GET", notes="設置年齡接口")
@GetMapping("/setage")
public void setAge(int age) {
this.age = age;
}

}

 

用swagger生成接口文檔代碼