• 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
    
  • 相关阅读:
    【JSOI 2008】 最大数
    【Usaco2008 Dec】Patting Heads
    【AHOI 2005】 约数研究
    【HAOI2007】反素数
    BZOJ3676 APIO2014回文串(manacher+后缀自动机)
    Luogu3804 【模板】后缀自动机(后缀自动机)
    后缀自动机学习笔记
    Codeforces ECR47F Dominant Indices(线段树合并)
    BZOJ1127 POI2008KUP(悬线法)
    BZOJ4652 NOI2016循环之美(莫比乌斯反演+杜教筛)
  • 原文地址:https://www.cnblogs.com/yonyong/p/11298994.html
Copyright © 2020-2023  润新知