1. 程式人生 > >使用io.swagger整合swagger與cxf

使用io.swagger整合swagger與cxf

io.swagger(1.5.2)整合cxf(2.7.18) spring(3.2.7.RELEASE) struts2(2.3.36)

一:參考文件:

https://github.com/swagger-api/swagger-samples/blob/master/java/java-jaxrs-cxf/src/main/webapp/index.html

https://github.com/swagger-api/swagger-samples/tree/master/java/java-resteasy-spring/src/main/webapp/WEB-INF

https://github.com/swagger-api/swagger-ui/tree/2.x/dist

二:配置

pom.xml

		<!-- swagger -->
		<dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-jaxrs</artifactId>
            <version>1.5.2</version>
            <exclusions>  
	            <exclusion>  
	                <groupId>javax.validation</groupId>
				    <artifactId>validation-api</artifactId>
	            </exclusion>
        	</exclusions> 
        </dependency>

複製https://github.com/swagger-api/swagger-ui/tree/2.x/dist的頁面到專案,並根據配置修改swagger.index.html的url

security.xml增加

    <http pattern="/**/*.json" security="none" />
cxf-service.xml增加

	<bean id="swaggerListingResource" class="io.swagger.jaxrs.listing.ApiListingResource"/>
	<bean id="swaggerWriter" class="io.swagger.jaxrs.listing.SwaggerSerializers"/>
	
	<bean id="beanConfig" class="io.swagger.jaxrs.config.BeanConfig">
        <property name="title" value="Swagger Sample App"/>
        <property name="version" value="1.0.0" />
        <property name="schemes" value="http" />
        <property name="host" value="localhost:8080" />
        <property name="basePath" value="xxxx"/>
        <property name="resourcePackage" value="com.x.x.x.x"/>
        <property name="scan" value="true"/>
    </bean>

	<jaxrs:server address="/swagger" id="swaggerApi">
		<jaxrs:serviceBeans>
			<ref bean="swaggerListingResource"></ref>
		</jaxrs:serviceBeans>
        <jaxrs:providers>
			<ref bean="swaggerWriter" />
		</jaxrs:providers>
	</jaxrs:server>

其中<property name="resourcePackage" value="com.x.x.x.x"/>是要提供服務介面的服務,使用@Path,@Api,@ApiOperation等註解

三:錯誤:

使用1.5.0報錯

Failed to scan JAR [file:/Users/wangql/deploy/gppmm/ppmm/WEB-INF/lib/jackson-dataformat-xml-2.4.2.jar] from WEB-INF/lib

java.util.zip.ZipException: error in opening zip file

使用1.5.9報錯

javax/validation/constraints/Size找不到

四:如果是cxf-3.x,可以參考

https://github.com/apache/cxf/tree/3.0.x-fixes/distribution/src/main/release/samples/jax_rs。

http://cxf.apache.org/docs/swagger2feature.htm

估計使用2.x的配置方式也可以實現。具體沒有測試過