springboot jersey中配置swagger2

1. pom.xml配置:

出去springboot和jersey陪之外需添加


            io.swagger
            swagger-jersey2-jaxrs
            1.5.21
        

        
            io.swagger
            swagger-annotations
            1.5.21
        

        
            io.swagger
            swagger-models
            1.5.21
        

        
        
            org.webjars
            swagger-ui
            2.2.10
        

        

 

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_第1张图片

 

4. application.properties中添加配置:

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

你可能感兴趣的:(java)