Swagger和Spring MVC整合

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

1、加入依赖

<!-- springfox依赖 -->
       <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${springfox.version}</version>
      </dependency>
      <dependency>
        <groupId>io.springfox</groupId>
       <artifactId>springfox-swagger-ui</artifactId>
        <version>${springfox.version}</version>
      </dependency>
      <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>15.0</version>
      </dependency>
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
      </dependency>

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

<!-- Enables swgger ui-->
     <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
     <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

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错误

<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
     <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

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();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值