Swagger和Spring MVC整合

参考文章地址:https://www.cnblogs.com/nihaofenghao/p/7633018.html

1、加入依赖


       
        io.springfox
        springfox-swagger2
        ${springfox.version}
      
      
        io.springfox
       springfox-swagger-ui
        ${springfox.version}
      
      
        com.google.guava
        guava
        15.0
      
      
        com.fasterxml.jackson.core
        jackson-databind
        ${jackson.version}
      

2、spring-mvc.xml 加入下面的代码


     
     

3、swagger2配置类

package com.swagger.controller.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.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;

@Configuration
@EnableWebMvc// spring mvc框架必须加上这个注释
@EnableSwagger2
@ComponentScan(basePackages="com.swagger.controller")
public class SwaggerConfig {
    
    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.swagger.controller"))//改成api接口所在包
                .paths(PathSelectors.any())
                .build();
    }
    

    private ApiInfo apiInfo(){
         @SuppressWarnings("deprecation")
        ApiInfo info=new ApiInfo(
                 "Spring 构建RestFule",
                 "aaa",
                 "aa",
                 "a",
                 "cc",
                 "x",
                 "x");
         return info;
    }
}

4、编写接口

package com.swagger.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("测试类")
@Controller
public class SwaggerController {
    
    @ApiOperation(value="测试方法",httpMethod="GET")
    @RequestMapping(value="/test",method=RequestMethod.GET)
    @ResponseBody
    public String hello(){
        return "hello word!!!";
    }

}

完毕。

小结:

1、@EnableWebMvc// spring mvc框架必须加上这个注释,第一次搜的例子是spring boot框架的,没有这句话,接口页面打开是空白的。

2、spring-mvc.xml中加入的两句话不要漏掉,我是漏掉了第一段,然后接口页面报404错误


     

3、记得改成api接口所在包 

public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.swagger.controller"))//改成api接口所在包
                .paths(PathSelectors.any())
                .build();
    }

你可能感兴趣的:(web后台,swagger)