swagger springmvc集成 生成restful api文档
https://blog.csdn.net/qq_359016
1、maven 所需jar包
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-models</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.3.11</version>
</dependency>
<!-- swagger-springmvc dependencies -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.4</version>
</dependency>
2、创建 SwaggerConfig配置文件
import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableSwagger
//@EnableWebMvc
public class SwaggerConfig {
private SpringSwaggerConfig springSwaggerConfig;
/**
* Required to autowire SpringSwaggerConfig
*/
@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)
{
this.springSwaggerConfig = springSwaggerConfig;
}
/**
* Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc
* framework - allowing for multiple swagger groups i.e. same code base
* multiple swagger resource listings.
*/
@Bean
public SwaggerSpringMvcPlugin customImplementation()
{
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
.apiInfo(apiInfo())
.includePatterns(".*?");
}
private ApiInfo apiInfo()
{
ApiInfo apiInfo = new ApiInfo(
"My Apps API Title",
"My Apps API Description",
"My Apps API terms of service",
"My Apps API Contact Email",
"My Apps API Licence Type",
"My Apps API License URL");
return apiInfo;
}
}
3、配置静态资源
在 spring-mvc.xml配置静态资源
<!-- 配置静态资源 -->
<mvc:resources mapping="/dist/**" location="/WEB-INF/dist/"/>
4、在代码中添加相关APIAnnotation
@ResponseBody
@RequestMapping( value = "addUser", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
@ApiOperation(value = "添加用户", httpMethod = "POST", response = BaseResultVo.class, notes = "add user")
public String addUser(@ApiParam(required = true, name = "postData", value = "用户信息json数据")@RequestParam( value = "postData") String postData,
HttpServletRequest request) {
LOGGER.debug(String.format("at function, %s", postData))if (null == postData || postData.isEmpty())
{returnsuper.buildFailedResultInfo(-1,"post data is empty!");}
UserInfo user = JSON.parseObject(postData, UserInfo.class);
int result = userService.addUser(user);
return buildSuccessResultInfo(result);
}
5、GitHub下载 Swagger UI
到 https://github.com/swagger-api/swagger-ui下载 Swagger ui 注意下载 Swagger 2.0 到 3.0之间的版本
解压之后 讲 dist包全部拷贝到自己项目 WEB-INF目录下;此时 dist下index.html 页面为:
6、打开浏览器 输入 http://localhost/{project}/dist/index.html#
将index.html 的url 修改为 :http://localhost/LCgame/api-docs
刷新浏览器出现
出现 detectedType.toLowerCase is not a function 的js错误;网上有几种说法:
1、说是fastjsonapi问题,升级到1.2.15版本以上就可以解决了 参考:http://blog.csdn.net/nihaoqiulinhe/article/details/68490133
2、不使用mvc:annontation,细化其配置
我试过这两种方法 都没用 最后将 swagger-ui.js 6365行进行修改:
重新刷新浏览器:
大功告成!!!