springboot整合swagger2.0
阿新 • • 發佈:2018-12-29
1、pom.xml新增依賴
<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>
2、建立swaggerConfig配置檔案
package com.conf;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResponseMessage;
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;
/**
* @author wanjiadong
* @description
* @date Create in 10:22 2018/12/29
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<>();
tokenPar.name("token").description("令牌")
.modelRef(new ModelRef("string")).parameterType("header").required(true).build();
pars.add(tokenPar.build());
List<ResponseMessage> responseMessageList = new ArrayList<>();
responseMessageList.add(new ResponseMessageBuilder().code(401).message("Unauthorized").responseModel(new ModelRef("DefaultResponse")).build());
responseMessageList.add(new ResponseMessageBuilder().code(403).message("Forbidden").responseModel(new ModelRef("DefaultResponse")).build());
responseMessageList.add(new ResponseMessageBuilder().code(404).message("Not Found").responseModel(new ModelRef("DefaultResponse")).build());
responseMessageList.add(new ResponseMessageBuilder().code(500).message("Server Error").responseModel(new ModelRef("DefaultResponse")).build());
return new Docket(DocumentationType.SWAGGER_2)
.enable(true)//專案上線並且需要關閉swagger介面,可以通過配置許可權
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.controller"))
.paths(PathSelectors.any())
.build()
.globalResponseMessage(RequestMethod.GET, responseMessageList)
.globalResponseMessage(RequestMethod.POST, responseMessageList)
.globalOperationParameters(pars);
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("test hah")
.description("description hah")
.termsOfServiceUrl("url")
.version("1.0")
//建立人
.contact(new Contact("wanjiadong", "", " [email protected]"))
.build();
}
}
3、使用
package com.controller; import com.pojo.Test1; import com.properties.Properties; import com.response.DefaultResponse; import com.service.IMyService; import com.util.BeanUtil; import com.util.BeanUtil1; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import reactor.core.publisher.Mono; import sun.misc.Request; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.net.SocketTimeoutException; /** * @author wanjiadong * @description * @date Create in 10:14 2018/9/4 */ @RestController @RequestMapping("/test") @Api(value = "testController", description = "測試類") //@EnableAutoConfiguration public class TestController implements ApplicationListener<ContextRefreshedEvent> { private ApplicationContext context = null; @Autowired private IMyService myService1; @Value("${test.server.name:haha}") private String testServerName; // @Autowired private Properties properties; @Autowired private BeanUtil1 beanUtil1; @RequestMapping("/sayHello") @ApiOperation(value = "說hello", notes = "just say hello", httpMethod = "GET") public DefaultResponse sayHello(HttpServletRequest request) { // myService1.sayHello(); // ContextLoader.getCurrentWebApplicationContext().getBean("properties"); Properties properties1 = BeanUtil.getBean("properties"); Properties properties2 = (Properties) context.getBean("properties"); Properties properties3 = (Properties) WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()).getBean("properties"); Properties properties4 = beanUtil1.getBean("properties"); return new DefaultResponse(); } // @GetMapping("/getWebFlux") // public Mono<String> getWebFlux() { // return Mono.just("getWebFlux"); // } @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { if(null != contextRefreshedEvent) { context = contextRefreshedEvent.getApplicationContext(); } } }