swagger配置
package com.sky.config;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* 使用swagger需要创建一个配置类,并开启swagger配置
* ps:swagger和 swagger2 不能通用
*
* 步骤:
* 1.将swaggerConfig注册到ioc容器中
* 2.开启swagger2功能
* 3.运行项目,访问swagger-ui.html
*/
@Configuration // 就是@Component
@EnableSwagger2 // 开启swagger
public class Swagger2Config {
}
那我们要怎么更改swagger的默认配置呢?
先看 @EnableSwagger2
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package springfox.documentation.swagger2.annotations;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;
import springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration;
@Retention(RetentionPolicy.RUNTIME)
@Target({
ElementType.TYPE})
@Documented
@Import({
Swagger2DocumentationConfiguration.class}) // 导入指定类
public @interface EnableSwagger2 {
}
该类的注解上有==@Import({Swagger2DocumentationConfiguration.class})==,导入了 Swagger2DocumentationConfiguration.class 这个类,继续看一下这个类是干哈的。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package springfox.documentation.swagger2.configuration;
import ...
@Configuration // 该类是一个配置类
@Import({
SpringfoxWebMvcConfiguration.class, SwaggerCommonConfiguration.class})
@ComponentScan(
basePackages = {
"springfox.documentation.swagger2.mappers"}
)
@ConditionalOnWebApplication
public class Swagger2DocumentationConfiguration {
public Swagger2DocumentationConfiguration() {
}
@Bean
public JacksonModuleRegistrar swagger2Module() {
return new Swagger2JacksonModule();
}
@Bean
public HandlerMapping swagger2ControllerMapping(Environment environment, DocumentationCache documentationCache, ServiceModelToSwagger2Mapper mapper, JsonSerializer jsonSerializer) {
return new PropertySourcedRequestMappingHandlerMapping(environment, new