@Api(value="用戶controller",tags={"用戶操做接口"}) @RestController public class UserController { }
@Api(value="UserController",tags={"用戶接口"}) @RestController public class UserController { @ApiOperation(value="獲取用戶信息",tags={"獲取用戶信息"},notes="注意") @GetMapping("/getUserInfo") public User getUserInfo(@ApiParam(name="id",value="用戶id",required=true) Long id,@ApiParam(name="username",value="用戶名") String username) {
User user = userService.getUserInfo(); return user; } }
@ApiModel(value="user",description="用戶對象") @Data public class User implements Serializable{ private static final long serialVersionUID = 1L; @ApiModelProperty(value="用戶名",name="username",example="xingguo") private String username; @ApiModelProperty(value="狀態",name="state",required=true) private Integer state; private String password; private String nickName; private Integer isDeleted; @ApiModelProperty(value="ids",hidden=true) private String[] ids; private List<String> idList; }
@ApiOperation("修改用戶信息") @PostMapping("/updateUserInfo") public int updateUserInfo(@RequestBody @ApiParam(name="用戶對象",value="json格式",required=true) User user){ int num = userService.updateUserInfo(user); return num; }
@ApiOperation("查詢測試") @GetMapping("select") //@ApiImplicitParam(name="name",value="用戶名",dataType="String", paramType = "query")
@ApiImplicitParams({ @ApiImplicitParam(name="name",value="用戶名",dataType="string", paramType = "query",example="xingguo"), @ApiImplicitParam(name="id",value="用戶id",dataType="long", paramType = "query")}) public void select(){ }
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 下面這個界面更好看,更好用-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.5</version>
</dependency>
import com.google.common.base.Predicates; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.ParameterBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.schema.ModelRef; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.service.Parameter; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; import java.util.List; /** * swagger-api 配置 * * @author wzm * @version 1.0.0 * @date 2019/6/15 **/ @Configuration @EnableSwagger2 @EnableSwaggerBootstrapUI public class Swagger2 { /** * http://localhost:8085/fabric-net/swagger-ui.html * http://localhost:8085/fabric-net/doc.html */
private static final String SWAGGER_SCAN_BUSINESS_PACKAGE = "com.thyc.fabric.controller.business"; private static final String BUSINESS_VERSION = "1.0.0"; private static final String SWAGGER_SCAN_FABRIC_PACKAGE = "com.thyc.fabric.controller.fabric"; private static final String FABRIC_VERSION = "1.0.0"; @Bean public Docket createBusinessApi() { List<Parameter> pars = new ArrayList<>(); ParameterBuilder ticketPar1 = new ParameterBuilder(); ticketPar1.name("Authorization").description("登陸令牌") .modelRef(new ModelRef("string")).parameterType("header") .required(false).build(); pars.add(ticketPar1.build()); return new Docket(DocumentationType.SWAGGER_2) .globalOperationParameters(pars) //分組名不支持中文
.groupName("business") .apiInfo(apiBusinessInfo()) .pathMapping("/") .select() // 對全部api進行監控
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BUSINESS_PACKAGE)) // 錯誤路徑不監控
.paths(Predicates.not(PathSelectors.regex("/error.*"))) // 對根下全部路徑進行監控
.paths(PathSelectors.regex("/.*")) .build(); } private ApiInfo apiBusinessInfo() { Contact contact = new Contact("thyc","thyc.com","thyc@email"); return new ApiInfoBuilder() //設置文檔的標題
.title("Business") //設置文檔的描述->1.Overview
.description("業務模塊數據管理") //設置文檔的版本信息-> 1.1 Version information
.termsOfServiceUrl("http://localhost:8085/fabric-net") .contact(contact) .version(BUSINESS_VERSION) .build(); } //------------------------------------------------------------------------------------------------------------------
@Bean public Docket createFabricApi() { List<Parameter> pars = new ArrayList<Parameter>(); ParameterBuilder ticketPar1 = new ParameterBuilder(); ticketPar1.name("Authorization").description("登陸令牌") .modelRef(new ModelRef("string")).parameterType("header") .required(false).build(); pars.add(ticketPar1.build()); return new Docket(DocumentationType.SWAGGER_2) .globalOperationParameters(pars) //分組名不支持中文
.groupName("fabric") .apiInfo(apiFabricInfo()) .pathMapping("/") .select() // 對全部api進行監控
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_FABRIC_PACKAGE)) // 錯誤路徑不監控
.paths(Predicates.not(PathSelectors.regex("/error.*"))) // 對根下全部路徑進行監控
.paths(PathSelectors.regex("/.*")) .build(); } private ApiInfo apiFabricInfo() { Contact contact = new Contact("thyc","thyc.com","thyc@email"); return new ApiInfoBuilder() //設置文檔的標題
.title("Fabric-Network") //設置文檔的描述->1.Overview
.description("超級帳本網絡信息管理") //設置文檔的版本信息-> 1.1 Version information
.termsOfServiceUrl("http://localhost:8085/fabric-net") .contact(contact) .version(FABRIC_VERSION) .build(); } }