SSM集成Swagger2

1.集成流程

1.引入依赖

<!--swagger-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

<!-- swagger依赖的工具包 -->
<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.12.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml</groupId>
            <artifactId>classmate</artifactId>
            <version>1.5.1</version>
        </dependency>

2.编写一个swagger配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 模块名称:Swagger配置文件
 * 模块类型:配置类
 * 编码人:高靖博
 * 创建时间:2023/2/27
 * 联系电话:18587388612
 */
@EnableSwagger2
@Configuration
public class Swagger2Config {
    
    

    @Bean
    public Docket createRestApi() {
    
    
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.huawei.ssmdemo2023"))//改写为你的根包路径
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
    
    
        return new ApiInfoBuilder()
                .title("华为昆明数字经济学院 实验项目")
                .termsOfServiceUrl("localhost:8080/ssm")
                .description("springmvc swagger2")
                .contact(new Contact("Gaojingbo", "http://www.baidu.com", "[email protected]"))
                .version("1.1")
                .build();
    }


}

3.配置swagger静态资源目录映射

<!-- 配置拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"></mvc:mapping>
            <!-- 除去部分请求不进入拦截器 -->
            <!-- 登录接口 -->
            <mvc:exclude-mapping path="/user/doLogin" />
            <!-- 获取验证码接口 -->
            <mvc:exclude-mapping path="/reqCode/getReqCode" />
            <!-- 查看swagger文档 -->
            <mvc:exclude-mapping path="/swagger-ui.html/**" />
            <mvc:exclude-mapping path="/webjars/**" />
            <mvc:exclude-mapping path="/swagger-resources/**" />
            <mvc:exclude-mapping path="/v2/**" />
            <bean class="com.huawei.ssmdemo2023.core.interceptors.LoginInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>

4.在controller类和方法上添加注解

@Api(description = "学生信息管理模块")
@Controller
@RequestMapping("/student")
@Slf4j //提供一个日志log对象注入到当前类
public class StudentController {
    
    

    // 注入业务层实例
    @Resource(name = "studentServiceImpl")
    private StudentService studentService;

    @ApiOperation(value = "获取学生信息通过学生id(GET)", notes = "查询记录:http method is get")
    @RequestMapping(value = "/getStudentByID",method = RequestMethod.GET)
    @ResponseBody
    public MyResult getStudentByID(@ApiParam(required = true, hidden = false, value = "学生id") String id){
    
    
        log.debug("学生信息管理:获取学生信息通过学生id [param:id"+id+"]");

        MyResult result = new MyResult();

        //调用业务层
        StudentDO studentDO = studentService.getStudentByID(id);

        if(studentDO==null){
    
    
            result.setCode(500);
            result.setMsg("没有查询到任何学生数据!");
        }else{
    
    
            result.setMsg("学生信息查询成功!");
            result.setData(studentDO);
        }

        return result;
    }
}

猜你喜欢

转载自blog.csdn.net/gjb760662328/article/details/129356801