Commit 159eaad1af4d103de7ebcd1127bcdf9f26e47607
1 parent
cc51d294
集成swagger
Showing
7 changed files
with
150 additions
and
3 deletions
pom.xml
| ... | ... | @@ -98,8 +98,21 @@ |
| 98 | 98 | <artifactId>fastjson</artifactId> |
| 99 | 99 | <version>1.2.47</version> |
| 100 | 100 | </dependency> |
| 101 | - | |
| 102 | - | |
| 101 | + <!--swagger2--> | |
| 102 | + <dependency> | |
| 103 | + <groupId>io.springfox</groupId> | |
| 104 | + <artifactId>springfox-swagger2</artifactId> | |
| 105 | + <version>2.7.0</version> | |
| 106 | + </dependency> | |
| 107 | + <dependency> | |
| 108 | + <groupId>io.springfox</groupId> | |
| 109 | + <artifactId>springfox-swagger-ui</artifactId> | |
| 110 | + <version>2.7.0</version> | |
| 111 | + </dependency> | |
| 112 | + <dependency> | |
| 113 | + <groupId>org.springframework.security</groupId> | |
| 114 | + <artifactId>spring-security-web</artifactId> | |
| 115 | + </dependency> | |
| 103 | 116 | |
| 104 | 117 | </dependencies> |
| 105 | 118 | ... | ... |
src/main/java/com/zteits/oa/DailyReportApplication.java
| ... | ... | @@ -6,6 +6,8 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 6 | 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 7 | 7 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; |
| 8 | 8 | import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; |
| 9 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; | |
| 10 | + | |
| 9 | 11 | |
| 10 | 12 | @SpringBootApplication |
| 11 | 13 | @EnableAutoConfiguration(exclude = { | ... | ... |
src/main/java/com/zteits/oa/configuration/WebMvcConfig.java
0 → 100644
| 1 | +package com.zteits.oa.configuration; | |
| 2 | + | |
| 3 | +import org.springframework.context.annotation.Configuration; | |
| 4 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; | |
| 5 | +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | |
| 6 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | |
| 7 | + | |
| 8 | +@Configuration | |
| 9 | +@EnableWebMvc | |
| 10 | +public class WebMvcConfig extends WebMvcConfigurerAdapter { | |
| 11 | + | |
| 12 | + @Override | |
| 13 | + public void addResourceHandlers(ResourceHandlerRegistry registry) { | |
| 14 | + registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); | |
| 15 | + | |
| 16 | + registry.addResourceHandler("swagger-ui.html") | |
| 17 | + .addResourceLocations("classpath:/META-INF/resources/"); | |
| 18 | + | |
| 19 | + registry.addResourceHandler("/webjars/**") | |
| 20 | + .addResourceLocations("classpath:/META-INF/resources/webjars/"); | |
| 21 | + | |
| 22 | + } | |
| 23 | +} | ... | ... |
src/main/java/com/zteits/oa/configuration/fiter/ResponseHeaderFilter.java
0 → 100644
| 1 | +package com.zteits.oa.configuration.fiter; | |
| 2 | + | |
| 3 | +import java.io.IOException; | |
| 4 | + | |
| 5 | +import javax.servlet.Filter; | |
| 6 | +import javax.servlet.FilterChain; | |
| 7 | +import javax.servlet.FilterConfig; | |
| 8 | +import javax.servlet.ServletException; | |
| 9 | +import javax.servlet.ServletRequest; | |
| 10 | +import javax.servlet.ServletResponse; | |
| 11 | +import javax.servlet.http.HttpServletResponse; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * 添加响应统一请求头 | |
| 15 | + * | |
| 16 | + * Copyright: Copyright (c) 2017 zteits | |
| 17 | + * | |
| 18 | + * @ClassName: ResponseHeaderFilter.java | |
| 19 | + * @Description: | |
| 20 | + * @version: v1.0.0 | |
| 21 | + * @author: zhaowg | |
| 22 | + * @date: 2017年5月8日 下午3:17:18 | |
| 23 | + * Modification History: | |
| 24 | + * Date Author Version Description | |
| 25 | + *---------------------------------------------------------* | |
| 26 | + * 2017年5月8日 zhaowg v1.0.0 创建 | |
| 27 | + */ | |
| 28 | +public class ResponseHeaderFilter implements Filter{ | |
| 29 | + | |
| 30 | + @Override | |
| 31 | + public void doFilter(ServletRequest request, ServletResponse response, | |
| 32 | + FilterChain chain) throws IOException, ServletException { | |
| 33 | + HttpServletResponse res = (HttpServletResponse) response; | |
| 34 | + res.addHeader("Access-Control-Allow-Origin", "*"); | |
| 35 | + res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); | |
| 36 | + res.addHeader("Access-Control-Allow-Headers", "Content-Type, X-Auth-Token, x-requested-with ,Authorization"); | |
| 37 | + chain.doFilter(request, response); | |
| 38 | + } | |
| 39 | + | |
| 40 | + @Override | |
| 41 | + public void destroy() { | |
| 42 | + } | |
| 43 | + | |
| 44 | + @Override | |
| 45 | + public void init(FilterConfig filterConfig) throws ServletException { | |
| 46 | + } | |
| 47 | +} | ... | ... |
src/main/java/com/zteits/oa/configuration/session/HttpSessionConfig.java
| ... | ... | @@ -18,7 +18,7 @@ import org.springframework.session.web.http.HttpSessionStrategy; |
| 18 | 18 | * ---------------------------------------------------------* |
| 19 | 19 | * 2017/5/16 atao v1.0.0 创建 |
| 20 | 20 | */ |
| 21 | -//maxInactiveIntervalInSeconds | |
| 21 | +//@maxInactiveIntervalInSeconds | |
| 22 | 22 | @EnableRedisHttpSession(maxInactiveIntervalInSeconds=30*24*60*60) |
| 23 | 23 | public class HttpSessionConfig { |
| 24 | 24 | ... | ... |
src/main/java/com/zteits/oa/configuration/swagger/SwaggerConfigAuto.java
0 → 100644
| 1 | +package com.zteits.oa.configuration.swagger; | |
| 2 | + | |
| 3 | +import org.springframework.context.annotation.Bean; | |
| 4 | +import org.springframework.context.annotation.ComponentScan; | |
| 5 | +import org.springframework.context.annotation.Configuration; | |
| 6 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; | |
| 7 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; | |
| 8 | + | |
| 9 | +import springfox.documentation.builders.ApiInfoBuilder; | |
| 10 | +import springfox.documentation.builders.PathSelectors; | |
| 11 | +import springfox.documentation.builders.RequestHandlerSelectors; | |
| 12 | +import springfox.documentation.service.ApiInfo; | |
| 13 | +import springfox.documentation.service.Contact; | |
| 14 | +import springfox.documentation.spi.DocumentationType; | |
| 15 | +import springfox.documentation.spring.web.plugins.Docket; | |
| 16 | +import springfox.documentation.swagger2.annotations.EnableSwagger2; | |
| 17 | +@Configuration // 配置注解,自动在本类上下文加载一些环境变量信息 | |
| 18 | +@EnableSwagger2 // 使swagger2生效 | |
| 19 | +public class SwaggerConfigAuto extends WebMvcConfigurationSupport{ | |
| 20 | + | |
| 21 | + @Bean | |
| 22 | + public Docket buildDocket(){ | |
| 23 | + return new Docket(DocumentationType.SWAGGER_2) | |
| 24 | + .apiInfo(buildApiInf()) | |
| 25 | + .select() .apis(RequestHandlerSelectors.basePackage("com.zteits.oa.report.web"))//controller路径 | |
| 26 | + .paths(PathSelectors.any()) | |
| 27 | + .build(); | |
| 28 | + } | |
| 29 | + | |
| 30 | + private ApiInfo buildApiInf(){ | |
| 31 | + return new ApiInfoBuilder() | |
| 32 | + .title("欢迎是用swagger2") | |
| 33 | + .termsOfServiceUrl("http://blog.csdn.net/u014231523网址链接") | |
| 34 | + .description("springmvc swagger2") | |
| 35 | + .contact(new Contact("diaoxingguo", "http://blog.csdn.net/u014231523", "diaoxingguo@163.com")) | |
| 36 | + .build(); | |
| 37 | + | |
| 38 | + } | |
| 39 | +} | ... | ... |
src/main/java/com/zteits/oa/report/web/demo/DemoController.java
0 → 100644
| 1 | +package com.zteits.oa.report.web.demo; | |
| 2 | + | |
| 3 | +import javax.servlet.http.HttpServletRequest; | |
| 4 | + | |
| 5 | +import org.springframework.web.bind.annotation.RequestBody; | |
| 6 | +import org.springframework.web.bind.annotation.RequestMapping; | |
| 7 | +import org.springframework.web.bind.annotation.RequestMethod; | |
| 8 | +import org.springframework.web.bind.annotation.RestController; | |
| 9 | + | |
| 10 | +import io.swagger.annotations.Api; | |
| 11 | + | |
| 12 | +@Api(value = "swagger测试", description = "swagger测") | |
| 13 | +@RestController | |
| 14 | +@RequestMapping("/demo") | |
| 15 | +public class DemoController { | |
| 16 | + | |
| 17 | + @RequestMapping(value="/demo",method = RequestMethod.POST) | |
| 18 | + public String test(@RequestBody String param, HttpServletRequest request){ | |
| 19 | + | |
| 20 | + return param; | |
| 21 | + } | |
| 22 | + | |
| 23 | +} | ... | ... |