SSM非springboot配置swagger2

Posted yonyong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM非springboot配置swagger2相关的知识,希望对你有一定的参考价值。

前提:maven,ssm,不是springboot项目

1.在maven中添加依赖

 <!-- Swagger2 Begin -->
    <!--springfox的核心jar包-->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.8.0</version>
    </dependency>
    <!--springfox-ui的jar包(里面包含了swagger的界面静态文件)-->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.8.0</version>
    </dependency>
    <!--springfox依赖的jar包;如果你的项目中已经集成了无需重复-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.0</version>
    </dependency>
    <!-- Swagger2 End -->

2.创建专门的swagger包用于存放swagger的配置文件,非必须,条理清晰罢了

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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;

/*重要!如果你的项目引入junit测试,此处需要使用@WebAppConfiguration,如果没有使用junit使用@Configuration(很多的博客都没有注明这个问题,为此我花了非常多的时间解决问题)*/
@WebAppConfiguration
@EnableSwagger2//重要!
@EnableWebMvc
@Configuration
public class Swagger2Config 
    @Bean
    public Docket createRestApi() 
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.yonyong.usetk.controller"))
                .paths(PathSelectors.any())
                .build();
    

    private ApiInfo apiInfo() 
        return new ApiInfoBuilder()
                .title("iToken API 文档")
                .description("iToken API 网关接口,http://www.funtl.com")
                .termsOfServiceUrl("http://www.faramita.online")
                .version("1.0.0")
                .build();
    

3.在springMVC的配置文件中添加swagger的配置

<!--将静态资源交由默认的servlet处理-->
<mvc:default-servlet-handler />
<!--向容器自动注入配置-->
<context:annotation-config />
<!-- 自动扫描  @Controller 与 swagger.java -->
<context:component-scan base-package="cn.yonyong.*.controller,cn.yonyong.usetk.config.swagger"/>
<!--重要!配置swagger资源不被拦截-->
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />

4.修改web.xml文件中配置所有的请求都经DispatcherServlet处理

<servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
  1. controller的配置
@Controller
@RequestMapping("/user")
@Api(value = "/user", tags = "User接口")
public class UserController 

    private static Logger logger = Logger.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/getUser/id",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据id获取用户信息", notes = "根据id获取用户信息", httpMethod = "GET", response = User.class)
    public ResponseEntity<User> getUser(@PathVariable int id)
        User user = userService.getUserById(id);
        logger.info("controller:"+user);
        return ResponseEntity.ok(user);
    
  1. 项目访问地址
//访问地址:[项目访问地址]/swagger-ui.html 
//例如本地Tomcat运行swagger-demo,那么访问地址为:http://localhost:8080/swagger-demo/swagger-ui.html

7.api注解的详细讲解

https://segmentfault.com/a/1190000010465989

以上是关于SSM非springboot配置swagger2的主要内容,如果未能解决你的问题,请参考以下文章

基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建

springboot项目配置swagger2示例

SpringBoot+Security+Swagger2配置

SpringBoot之Swagger2

ssm和springboot的区别

springboot配置swagger2