这是我的web层的目录,红框里的是需要改动的文件。
第一步:在web层的pom.xml文件里,添加所需jar包的依赖。
备注:添加之前可以先搜索一下,查看是否已经有了。
第二步:在源码目录下创建config的包,在包里创建SwaggerConfig文件。我的路径是com.schoolface.shkt的同级,具体可参考此博客的第一张图。
package com.schoolface.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
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;
/**
* Created by Silvia on 2018/11/20.
*/
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages = "com.schoolface.shkt.controller")
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("XXX项目接口文档")
.description("XXX项目接口测试")
.version("1.0.0")
.termsOfServiceUrl("")
.license("")
.licenseUrl("")
.build();
}
}
第三步:在springmvc的配置文件springmvc.xml文件中配置swagger。
备注:(1)最后一句和参考博客不同;(2)其中的base-package和class分别为controller层和swagger配置文件的路径,根据实际填写即可。
第四步:在web.xml文件中,添加如下内容。使请求都经DispatcherServlet处理。
备注:一般这个配置在web.xml中已经存在
第五步:确保controller层有相关的注解,@ApiOperation等。
第六步:在WEB-INF包下,新建swagger包,然后把swagger-ui相关的东西复制到swagger包下。swagger-ui云盘分享如下。
链接:https://pan.baidu.com/s/1hRoTuHBvE0V9QcfhDFojZg
提取码:npfb
找这个包的过程有些曲折,好多都不能用,似乎是版本的问题。
第七步:运行项目,在浏览器中访问
至此就完成了。在一些小细节上可能有些出入,但大体思路如此。欢迎读者交流。
参考博客:
https://www.cnblogs.com/jtlgb/p/6734177.html
https://blog.youkuaiyun.com/twomr/article/details/77101092