看到http://git.oschina.net/didispace/SpringBoot-Learning 的时候突然发现一个好玩的东西swagger2,是spring boot整合的。那么不用spring boot有没有办法呢,突然一下来感觉了就试试了。肯定是可以的哈哈。 首先是配置jar包。推荐maven
<!-- 添加Swagger2依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
然后是配置代码,其实网上可以找到很多,但是为了记录完整性就复制粘贴吧。
@Controller
@EnableSwagger2
public class Swagger {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.z201.rest"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Mvc中使用Swagger2构建RESTful APIs")
.description("osgit:http://git.oschina.net/Z201")
.termsOfServiceUrl("os博客 http://my.oschina.net/u/1791398")
.version("1.0")
.build();
}
}
其实很简单就是被MVC扫到就可以了。。。@Controller 一定是被SpringMvc扫到啊~
来源:oschina
链接:https://my.oschina.net/u/1791398/blog/727549