Commit aaea7e90 by fsn

集成swagger

parent 03c1a4c0
...@@ -134,6 +134,16 @@ ...@@ -134,6 +134,16 @@
<artifactId>beetl</artifactId> <artifactId>beetl</artifactId>
<version>2.7.15</version> <version>2.7.15</version>
</dependency> </dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies> </dependencies>
......
package com.stylefeng.guns; package com.stylefeng.guns;
import com.stylefeng.guns.config.properties.GunsProperties;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/** /**
* SpringBoot方式启动类 * SpringBoot方式启动类
...@@ -12,10 +16,21 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; ...@@ -12,10 +16,21 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @Date 2017/5/21 12:06 * @Date 2017/5/21 12:06
*/ */
@SpringBootApplication @SpringBootApplication
public class GunsApplication { public class GunsApplication extends WebMvcConfigurerAdapter{
protected final static Logger logger = LoggerFactory.getLogger(GunsApplication.class); protected final static Logger logger = LoggerFactory.getLogger(GunsApplication.class);
@Autowired
GunsProperties gunsProperties;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if(gunsProperties.getSwaggerOpen()){
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(GunsApplication.class, args); SpringApplication.run(GunsApplication.class, args);
logger.info("GunsApplication is sussess!"); logger.info("GunsApplication is sussess!");
......
package com.stylefeng.guns.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
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;
/**
* swagger配置类
*
* @author fengshuonan
* @date 2017年6月1日19:42:59
*/
@Configuration
@EnableSwagger2
@ConditionalOnProperty(prefix = "guns", name = "swagger-open", havingValue = "true")
public class SwaggerConfig{
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.stylefeng.guns.modular.system.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Guns RESTful APIs")
.description("Guns Api文档")
.termsOfServiceUrl("http://git.oschina.net/naan1993/guns")
.contact("stylefeng")
.version("2.0")
.build();
}
}
...@@ -20,7 +20,9 @@ public class GunsProperties { ...@@ -20,7 +20,9 @@ public class GunsProperties {
public static final String PREFIX = "guns"; public static final String PREFIX = "guns";
private Boolean kaptchaOpen; private Boolean kaptchaOpen = false;
private Boolean swaggerOpen = false;
private String fileUploadPath; private String fileUploadPath;
...@@ -56,4 +58,12 @@ public class GunsProperties { ...@@ -56,4 +58,12 @@ public class GunsProperties {
public void setKaptchaOpen(Boolean kaptchaOpen) { public void setKaptchaOpen(Boolean kaptchaOpen) {
this.kaptchaOpen = kaptchaOpen; this.kaptchaOpen = kaptchaOpen;
} }
public Boolean getSwaggerOpen() {
return swaggerOpen;
}
public void setSwaggerOpen(Boolean swaggerOpen) {
this.swaggerOpen = swaggerOpen;
}
} }
...@@ -16,7 +16,6 @@ import org.springframework.context.annotation.Configuration; ...@@ -16,7 +16,6 @@ import org.springframework.context.annotation.Configuration;
* @date 2016年11月12日 下午5:03:32 * @date 2016年11月12日 下午5:03:32
*/ */
@Configuration @Configuration
@ConfigurationProperties(prefix = "spring.mvc.view")
public class BeetlConfig { public class BeetlConfig {
@Autowired @Autowired
......
...@@ -7,6 +7,9 @@ import com.stylefeng.guns.core.template.config.ContextConfig; ...@@ -7,6 +7,9 @@ import com.stylefeng.guns.core.template.config.ContextConfig;
import com.stylefeng.guns.core.template.engine.SimpleTemplateEngine; import com.stylefeng.guns.core.template.engine.SimpleTemplateEngine;
import com.stylefeng.guns.core.template.engine.base.GunsTemplateEngine; import com.stylefeng.guns.core.template.engine.base.GunsTemplateEngine;
import com.stylefeng.guns.core.util.ToolUtil; import com.stylefeng.guns.core.util.ToolUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
...@@ -34,16 +37,22 @@ public class CodeController extends BaseController { ...@@ -34,16 +37,22 @@ public class CodeController extends BaseController {
/** /**
* 代码生成 * 代码生成
*/ */
@ApiOperation("生成代码")
@ApiImplicitParams({
@ApiImplicitParam(name = "bizChName", value = "业务名称", required = true, dataType = "String"),
@ApiImplicitParam(name = "bizEnName", value = "业务英文名称", required = true, dataType = "String"),
@ApiImplicitParam(name = "path", value = "项目生成类路径", required = true, dataType = "String")
})
@RequestMapping(value = "/generate") @RequestMapping(value = "/generate")
@ResponseBody @ResponseBody
public Object add(String bizChName, String bizEnName, String path) { public Object add(String bizChName, String bizEnName, String path) {
if(ToolUtil.isOneEmpty(bizChName,bizEnName)){ if (ToolUtil.isOneEmpty(bizChName, bizEnName)) {
throw new BussinessException(BizExceptionEnum.REQUEST_NULL); throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
} }
ContextConfig contextConfig = new ContextConfig(); ContextConfig contextConfig = new ContextConfig();
contextConfig.setBizChName(bizChName); contextConfig.setBizChName(bizChName);
contextConfig.setBizEnName(bizEnName); contextConfig.setBizEnName(bizEnName);
if(ToolUtil.isNotEmpty(path)){ if (ToolUtil.isNotEmpty(path)) {
contextConfig.setProjectPath(path); contextConfig.setProjectPath(path);
} }
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
################### guns配置 ################### ################### guns配置 ###################
guns: guns:
swagger-open: true #是否开启swagger (true/false)
kaptcha-open: false #是否开启登录时验证码 (true/false) kaptcha-open: false #是否开启登录时验证码 (true/false)
#file-upload-path: d:/tmp #文件上传目录(不配置的话为java.io.tmpdir目录) #file-upload-path: d:/tmp #文件上传目录(不配置的话为java.io.tmpdir目录)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment