spring cxf rs 集成swagger2

主要是在原有的spring+cxf rs 的基础上添加swagger2,整个过程很坎坷,先是上网找教程,各种教程,各种版本,各种坑,很头疼,最后没办法,直接照着官网来搞了。先说下我的版本:

  1. spring 4.3
  2. apache cxf 3.2.4
  3. swagger-ui 2.1.8-M1
  4. cxf-rt-rs-service-description-swagger 3.1.7
  5. jack.version 2.9.5

参考官网地址:
官方文档
spring集成demo

具体细节直接看pom里面只列出cxf和swagger的相关jar包:


<dependency>
    <groupId>org.apache.cxfgroupId>
    <artifactId>cxf-rt-rs-service-description-swaggerartifactId>
    <version>3.1.7version>
dependency>
<dependency>
    <groupId>org.webjarsgroupId>
    <artifactId>swagger-uiartifactId>
    <version>2.1.8-M1version>
dependency>


<dependency>
            <groupId>org.apache.cxfgroupId>
            <artifactId>cxf-rt-frontend-jaxrsartifactId>
            <version>${cxf.version}version>
        dependency>
        <dependency>
            <groupId>org.apache.cxfgroupId>
            <artifactId>cxf-rt-transports-http-jettyartifactId>
            <version>${cxf.version}version>
        dependency>
        <dependency>
            <groupId>org.apache.cxfgroupId>
            <artifactId>cxf-rt-rs-service-descriptionartifactId>
            <version>${cxf.version}version>
        dependency>

        
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrsgroupId>
            <artifactId>jackson-jaxrs-json-providerartifactId>
            <version>${jackson.version}version>
        dependency>

spring 配置文件:

<bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />
    <bean id="exceptionProvider" class="com.panku.web.common.CustExceptionMapper" /> 
     
    <bean id="swagger2Feature" class="org.apache.cxf.jaxrs.swagger.Swagger2Feature">
        
        <property name="title" value="xxx服务端接口"/>
        <property name="scan" value="true" />
        <property name="basePath" value="/wxService/webService/api">property>
        <property name="contact" value="xxx" />
        <property name="version" value="1.0">property>
        <property name="description" value="xxx">property>
        <property name="license" value="xxx">property>
        <property name="licenseUrl" value="xxx">property>
    bean>
    
    <jaxrs:server address="/api">
        <jaxrs:serviceBeans>
            <bean class="com.panku.web.service.wadl.impl.TestRsServiceImpl" />
        jaxrs:serviceBeans>
        <jaxrs:providers>
            <ref bean="jsonProvider" />
            <ref bean="exceptionProvider"/>
        jaxrs:providers>
        <jaxrs:features>
            <ref bean="swagger2Feature" />
        jaxrs:features>
    jaxrs:server>

Resource Demo

package com.panku.web.service.wadl.impl;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;

import com.panku.web.common.ResultMessage;
import com.panku.web.service.his.IBindUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
@Api(value = "/rest", description = "测试服务")
@Path("/rest")
public class TestRsServiceImpl  {

    @Autowired
    private IBindUserService bindUserService;   

    @ApiOperation(
            value = "/getBindUser/{openid}/{shenfzh}",
            notes = "获取当前绑定的用户",
            response = ResultMessage.class
     )
    @GET
    @Path("/getBindUser/{openid}/{shenfzh}")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultMessage getBindUser(@ApiParam("openid")@PathParam("openid")String openid, 
            @ApiParam("shenfzh")@PathParam("shenfzh")String shenfzh) {
        return ResultMessage.ok(null, bindUserService.getBindUser(openid, shenfzh));
    }
}

访问地址:直接输入你的服务地址,比如我servlet-mapping配置的是/webservice,那么我的访问地址就是:http://localhost:port/contexPath/webservice,之后展示出来两个地址,一个是服务的地址,另外一个是swagger 接口的地址,如下
spring cxf rs 集成swagger2_第1张图片
spring cxf rs 集成swagger2_第2张图片

欢迎指正~!

你可能感兴趣的:(spring cxf rs 集成swagger2)