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包:

<!--swagger-->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-rs-service-description-swagger</artifactId>
    <version>3.1.7</version>
</dependency>
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>swagger-ui</artifactId>
    <version>2.1.8-M1</version>
</dependency>

<!--cxf-->
<dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-service-description</artifactId>
            <version>${cxf.version}</version>
        </dependency>

        <!-- Jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <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" /> 
    <!-- CXF Swagger2Feature --> 
    <bean id="swagger2Feature" class="org.apache.cxf.jaxrs.swagger.Swagger2Feature">
        <!-- customize some of the properties -->
        <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>
    <!-- his -->
    <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 接口的地址,如下
这里写图片描述
这里写图片描述

欢迎指正~!

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值