SpringBoot集成Swagger

SpringBoot集成Swagger

第一步 引入Swagger的jar包

    //引入swagger2jar
    implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
    implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
  swagger配置类
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            //是否开启 (true 开启  false隐藏。生产环境建议隐藏)
            //.enable(false)
            .select()
            //扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api
            .apis(RequestHandlerSelectors.basePackage("com.shop.controller"))
            //指定路径处理PathSelectors.any()代表所有的路径
            .paths(PathSelectors.any())
            .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            //设置文档标题(API名称)
            .title("shop接口测试")
            //文档描述
            .description("接口说明")
            //服务条款URL
//            .termsOfServiceUrl("http://localhost:8090/")
            //版本号
            .version("1.0.0")
            .build();
    }
}

第三步 修改Controller

import com.shop.service.WeatherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/web/QueryWeather")
@Api(value = "天气接口",tags = "天气相关接口", description = "对接第三方中国天气网查询天气")
public class WeatherController {

    @Autowired
    private WeatherService weatherService;

    @GetMapping("/QueryDayWeather")
    @ApiOperation(value = "查询天气",notes = "查询天气",produces = "application/json")
    public Map QueryWeather() {
        return weatherService.queryWeather();
    }

}

第四步 Swagger页面访问

运行项目,输入http://localhost:8090/swagger-ui.html访问Swagger页面,页面如下





这里这些了部分接口进行测试,可以根据项目需求自行添加其他接口。

第五步 shiro或这SpringSecurity中配置

如果配置了spring secyrity的话

 @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .csrf().disable()
      .authorizeRequests()
      // .antMatchers("/shop/**", "/resources/**", "/swagger-ui.html", "/swagger-resources/**", "/v2/api-docs").permitAll() // 如果swagger提供认证功能,这里需要对swagger的资源地址放行,因为我这里swagger没有提供认证功能,所以全部都需要进行使用spring-security认证后才能访问
      .antMatchers("/shop/**", "/login/**", "/logout/**").permitAll() // 定义的地址不需要认证便可以访问
      .anyRequest().authenticated() // 其它未定义的,全部需要认证后才能访问
      .and()
      .formLogin() // 使用表单登录
      .and()
      .logout()
      .logoutSuccessUrl("/login"); // 退出登录后,跳转到登录
  }

如果配置了shiro的话

 filterMap.put("/swagger-ui.html", "anon");
 filterMap.put("/swagger-resources/**", "anon");
 filterMap.put("/v2/**", "anon");
 filterMap.put("/webjars/**", "anon");
posted @ 2021-11-06 17:08  dnydys  阅读(195)  评论(0)    收藏  举报