时间:2023-07-28 19:48:01 | 来源:网站运营
时间:2023-07-28 19:48:01 来源:网站运营
SpringBoot入门建站全系列(十六)整合Swagger文档中心:<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>
package com.cff.springbootwork.swagger.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.PathSelectors;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@EnableSwagger2public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.cff.springbootwork")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API文档") .description("简单优雅的restful风格") .version("1.0") .build(); }}
这里,@EnableSwagger2开启Swagger。@Configuration注解声明这里可以配置bean。package com.cff.springbootwork.swagger.config;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.List;import java.util.Set;import org.springframework.http.HttpMethod;import org.springframework.stereotype.Component;import com.cff.springbootwork.swagger.entity.TestEntity;import com.fasterxml.classmate.TypeResolver;import springfox.documentation.builders.OperationBuilder;import springfox.documentation.builders.ParameterBuilder;import springfox.documentation.builders.ResponseMessageBuilder;import springfox.documentation.schema.ModelRef;import springfox.documentation.service.ApiDescription;import springfox.documentation.service.ResponseMessage;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spi.service.ApiListingScannerPlugin;import springfox.documentation.spi.service.contexts.DocumentationContext;import springfox.documentation.spring.web.readers.operation.CachingOperationNameGenerator;/** * 该文件可有可无,手动增加接口的方法 * @author fufei * */@Componentpublic class SwaggerAddtionScanConfig implements ApiListingScannerPlugin { @Override public List<ApiDescription> apply(DocumentationContext documentationContext) { return new ArrayList<>( Arrays.asList( new ApiDescription("Login", "/login", "登录接口", Collections .singletonList( new OperationBuilder(new CachingOperationNameGenerator()).authorizations(new ArrayList<>()) .method(HttpMethod.POST) .summary("登录接口") .notes("登录接口")//方法描述 .tags(Collections.singleton("登录登出模块"))//归类标签 .produces(Collections.singleton("application/json")) .consumes(Collections.singleton("application/json")) .parameters( Arrays.asList( new ParameterBuilder() .description("email") .type(new TypeResolver() .resolve(String.class)) .name("email") .parameterType("query") .parameterAccess("access") .required(true) .modelRef(new ModelRef("string")) .build(), new ParameterBuilder() .description("password") .type(new TypeResolver() .resolve(String.class)) .name("password") .parameterType("query") .parameterAccess("access") .required(true) .modelRef(new ModelRef("string")) .build() )) .responseMessages(responseMessages()) .build()), false), new ApiDescription("Login", "/logout", "登出接口", Collections .singletonList( new OperationBuilder(new CachingOperationNameGenerator()).authorizations(new ArrayList<>()) .method(HttpMethod.POST) .summary("登出接口") .notes("登出接口")//方法描述 .tags(Collections.singleton("登录登出模块"))//归类标签 .produces(Collections.singleton("application/json")) .parameters( Collections .singletonList(new ParameterBuilder() .description("token") .type(new TypeResolver() .resolve(String.class)) .name("token") .parameterType("query") .parameterAccess("access") .required(true) .modelRef(new ModelRef("string")) .build())) .responseMessages(responseMessages()) .build()), false))); } /** * @return Set of response messages that overide the default/global response * messages */ private Set<ResponseMessage> responseMessages() { // <8> return Collections.singleton( new ResponseMessageBuilder().code(200).message("Successfully received bug 1767 or 2219 response") .responseModel(new ModelRef( TestEntity.class.getSimpleName()) ).build()); } @Override public boolean supports(DocumentationType documentationType) { return DocumentationType.SWAGGER_2.equals(documentationType); }}
package com.cff.springbootwork.swagger.web;import java.util.UUID;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import com.cff.springbootwork.swagger.entity.TestEntity;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;@Api(tags = {"测试模块"}, produces = "application/json")@RestController@RequestMapping("/swagger")public class SwaggerRest { @ApiOperation(value = "测试swagger的功能") @RequestMapping(value = "/test", method = { RequestMethod.GET }) public TestEntity test(@RequestParam String reqType) { String uuid = UUID.randomUUID().toString(); String welMsg = "welcome 程序猿"; if (reqType != null && "1000".equals(reqType)) { welMsg = "welcome 程序媛"; } TestEntity welEntity = new TestEntity(); welEntity.setUuid(uuid); welEntity.setWelMsg(welMsg); return welEntity; } @RequestMapping(value = "/test2", method = { RequestMethod.GET }) public TestEntity test2(@RequestParam String reqType) { String uuid = UUID.randomUUID().toString(); String welMsg = "welcome 程序猿"; if (reqType != null && "1000".equals(reqType)) { welMsg = "welcome 程序媛"; } TestEntity welEntity = new TestEntity(); welEntity.setUuid(uuid); welEntity.setWelMsg(welMsg); return welEntity; }}
package com.cff.springbootwork.swagger.entity;public class TestEntity { private String uuid; private String welMsg; public String getUuid() { return uuid; } public void setUuid(String uuid) { this.uuid = uuid; } public String getWelMsg() { return welMsg; } public void setWelMsg(String welMsg) { this.welMsg = welMsg; }}
关键词:中心,入门,系列