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", "", "664138205@qq.com"))
.build();
}
}
如果继承了WebMvcConfigurationSupport,则需要对资源做处理:
package com.webconfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* @author wanjiadong
* @description
* @date Create in 15:39 2019/1/5
*/
@Configuration
public class WebAppConfig extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}
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();
}
}
}