1. 程式人生 > >springboot jersey中配置swagger2

springboot jersey中配置swagger2

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包中的路徑)

 

4. application.properties中新增配置:

#--------------swagger文件配置
spring.jersey.application-path =/v1
springfox.documentation.swagger.v2.host=http://專案地址:專案埠
springfox.documentation.swagger.v2.path=/swagger/api-docs