Spring mvc整合swagger2。

和自甴很熟 提交于 2019-11-29 15:14:21

看到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扫到啊~

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!