springboot jersey中配置swagger2

时间:2024-04-01 14:59:19

1. pom.xml配置:

出去springboot和jersey陪之外需添加

<dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-jersey2-jaxrs</artifactId>
            <version>1.5.21</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.21</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.21</version>
        </dependency>

        <!-- swagger 静态资源 -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>swagger-ui</artifactId>
            <version>2.2.10</version>
        </dependency>
        <!-- springboot 集成 jersey 、swagger 实现 JAX-RS Restful 結束 -->

 

2. jersey配置类

import javax.annotation.PostConstruct;
import javax.ws.rs.ApplicationPath;

import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spring.scope.RequestContextFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.icwant.serv.exception.ICwantExceptionMapper;
import com.icwant.serv.tools.PropUtil;

import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.jaxrs.listing.ApiListingResource;
import io.swagger.jaxrs.listing.SwaggerSerializers;

@Component
@ApplicationPath("/v1")
public class ApplicationResConfigure extends ResourceConfig {

    @Value("${spring.jersey.application-path}")
    private String api_version;
    
    @Value("${spring.jersey.application-path}")
    private String apiPath;
    
    @Autowired
    private PropUtil propUtil;

    /**
     * Register JAX-RS application components.
     */
    public ApplicationResConfigure() {
        this.register(ICwantExceptionMapper.class);
        // this.register(ValidateException.class);
        this.register(RequestContextFilter.class);
        this.register(DynamicRolesDynamicFeature.class);
        // Use this for registering a full set of resources.
        this.packages("com.icwant.serv.controller");
        // JacksonJsonProvider jsonP = new JacksonJaxbJsonProvider().configure(
        // SerializationConfig.Feature.WRITE_EMPTY_JSON_ARRAYS, false);
        // register(SerializationConfig.Feature.WRITE_EMPTY_JSON_ARRAYS);
        register(JacksonFeature.class); // 注册JSON解析器\
    }

    @PostConstruct
    public void init() {
        // Register components where DI is needed
        if(!"production".equals(propUtil.getVersion()))
            this.configureSwagger();
    }

    private void configureSwagger() {
        // Available at localhost:port/swagger.json
        this.register(ApiListingResource.class);
        this.register(SwaggerSerializers.class);
        BeanConfig config = new BeanConfig();
        config.setConfigId("数据服务接口文档");
        config.setTitle("数据服务接口文档");
        config.setVersion(api_version);
        config.setContact("jared He");
        config.setSchemes(new String[] { "http", "https" });
        config.setBasePath(this.apiPath);
        config.setResourcePackage("com.icwant.serv.controller");
        config.setPrettyPrint(true);
        config.setScan(true);
    }
}

 

3. 静态资源配置:将下图所示的jar包中的index.html放入static文件夹下并修改其中的文件引用到正确位置(jar包中的路径)

springboot jersey中配置swagger2

 

4. application.properties中添加配置:

#--------------swagger文档配置
spring.jersey.application-path =/v1
springfox.documentation.swagger.v2.host=http://项目地址:项目端口
springfox.documentation.swagger.v2.path=/swagger/api-docs