springboot 2.3 如何整合swagger文档

1.添加swagger依赖

	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger2</artifactId>
		<version>2.9.2</version>
	</dependency>

	<!-- 接口API生成html文档 -->
	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger-ui</artifactId>
		<version>2.9.2</version>
	</dependency>
	
	<!--解决控制层接受参数为Integer类似的参数,访问swagger页面报错-->
	 <dependency>
       <groupId>io.swagger</groupId>
       <artifactId>swagger-models</artifactId>
       <version>1.5.21</version>
    </dependency>

2.在项目启动类的同级处添加swagger配置类

package com.iflytek.edu.hnezzhxy;

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


/**
 * @description swagger2文档
 * @version 1.0
 * @create 2020/06/19 08:31
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //"com.iflytek.edu.hnezzhxy.controller", web层所在的目录
                .apis(RequestHandlerSelectors.basePackage("com.iflytek.edu.hnezzhxy.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot利用swagger构建api文档---淮南第二中学招生系统")
                .description("简单优雅的restfun风格")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决静态资源无法访问
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        // 解决swagger无法访问
        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}

3.控制层添加一个方法

package com.iflytek.edu.hnezzhxy.controller;

import com.iflytek.edu.hnezzhxy.common.enums.ResponseCodeEnum;
import com.iflytek.edu.hnezzhxy.dao.ZsbmUserDao;
import com.iflytek.edu.hnezzhxy.model.ZsbmUser;
import com.iflytek.edu.hnezzhxy.util.ResponseResultUtil;
import com.iflytek.edu.hnezzhxy.vo.ResultVO;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpSession;

/**
 * @description 登陆处理类
 * @create 2020/06/18 15:44
 * @version 1.0
 */
@RestController
@Validated
@Api(tags = "登陆相关接口")
public class LoginController {
    /**
     * @description 退出登录
     * @param session
     * @return ResultVO
     */
    @RequestMapping("/getLoginOut")
    @ApiOperation(value="退出登陆",httpMethod ="GET", notes="退出登陆")
    public ResultVO getLoginOut(HttpSession session){
        Object object = session.getAttribute(Constants.SESSION_USER_Attribute);
        if(object!=null){
                session.removeAttribute(Constants.SESSION_USER_Attribute);
        }
        return  new ResponseResultUtil().success(ResponseCodeEnum.LOGINOUT_SUCCESS.getCode(),
                ResponseCodeEnum.LOGINOUT_SUCCESS.getMessage(),null,true);
    }

}

4.访问swagger文档

http://localhost:8081/hnezzsbm/swagger-ui.html#

5.效果图
在这里插入图片描述6友情提示
若你的swgger文档接口说明不是中文。请在对应的controller层添加tags=“你的中文描述”,若是配置了此中文,swagger接口详情信息点不进去,那么请将你的swgger依赖版本更新至2.8以上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值