Spring 、 CXF 整合 swagger 【试炼】

官网:http://swagger.io/

http://swagger.io/specification/

上面就是描述了什么是

SWAGGER OBJECT

2. 如何用jax-rs 注解方式产生

SWAGGER CORE

3.如何更好的展现

SWAGGER UI

4.如何开始

找找demo 按部就班 在这里 在这里

The samples have moved to a new repository and contain various integrations and configurations.

然后我下载了 java-jaxrs-cxf

修改了pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-java-cxf-sample</artifactId>
    <packaging>war</packaging>
    <name>swagger-java-cxf-sample</name>
    <version>1.0.0</version>

    <dependencies>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-client</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-service-description</artifactId>
            <version>3.0.0-milestone1</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-jaxrs</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>

        <!-- Swagger Stuff -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-jaxrs</artifactId>
            <version>1.5.9</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>

    <properties>
        <java-version>1.6</java-version>
        <springframework-version>3.2.1.RELEASE</springframework-version>
        <cxf-version>2.7.15</cxf-version>
        <jackson.version>1.8.6</jackson.version>
        <test.server.port>8080</test.server.port>
    </properties>
    <build>
        <finalName>swagger-java-cxf-sample</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

修改了spring 配置文件

<property name="host" value="localhost:8080/swagger-java-cxf-sample"/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core"
  xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:util="http://www.springframework.org/schema/util"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">

  <import resource="classpath:META-INF/cxf/cxf.xml" />

  <context:component-scan base-package="io.swagger.sample" />

  <!-- Swagger writers -->
  <bean id="swaggerWriter" class="io.swagger.jaxrs.listing.SwaggerSerializers" />
  <!-- <bean id="apiWriter" class="io.swagger.jaxrs.listing.ApiDeclarationProvider" /> -->

  <!-- JSON mapper -->
  <bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider" />

  <!-- Exception mapper -->
  <bean id="exceptionMapper" class="io.swagger.sample.resource.SampleExceptionMapper" />

  <!-- Application resources -->
  <bean id="userResource" class="io.swagger.sample.resource.UserResource" />
  <bean id="petResource" class="io.swagger.sample.resource.PetResource" />
  <bean id="petStoreResource" class="io.swagger.sample.resource.PetStoreResource" />

  <!-- Swagger API listing resource -->
  <bean id="swaggerResource" class="io.swagger.jaxrs.listing.ApiListingResource" />

  <cxf:bus>
    <cxf:features>
      <cxf:logging />
    </cxf:features>
  </cxf:bus>

  <!-- this scans the classes for resources -->
  <bean id="swaggerConfig" class="io.swagger.jaxrs.config.BeanConfig">
    <property name="resourcePackage" value="io.swagger.sample.resource"/>
    <property name="version" value="1.0.0"/>
    <property name="host" value="localhost:8080/swagger-java-cxf-sample"/>
    <property name="basePath" value="/api"/>
    <property name="title" value="Swagger Petstore"/>
    <property name="description" value="This is a app."/>
    <property name="contact" value="[email protected]"/>
    <property name="license" value="Apache 2.0"/>
    <property name="licenseUrl" value="http://www.apache.org/licenses/LICENSE-2.0.html"/>
    <!-- <property name="filterClass" value="ApiAuthorizationFilterImpl"/> -->
    <property name="scan" value="true"/>
  </bean>

  <bean class="org.apache.cxf.jaxrs.JAXRSServerFactoryBean" init-method="create">
    <property name="address" value="/" />
    <property name="serviceBeans">
      <list>
        <ref bean="userResource" />
        <ref bean="petResource" />
        <ref bean="petStoreResource" />

        <ref bean="swaggerResource" />
      </list>
    </property>
    <property name="providers">
      <list>
        <ref bean="jsonProvider" />
        <ref bean="swaggerWriter" />
        <ref bean="exceptionMapper" />

        <!-- required for writing swagger classes -->
        <!-- <ref bean="resourceWriter" />
        <ref bean="apiWriter" /> -->
      </list>
    </property>
  </bean>
</beans>

修改了ui文件

url = "http://localhost:8080/swagger-java-cxf-sample/api/swagger.json";
<!DOCTYPE html>
<html>
<head>
  <title>Swagger UI</title>
  <link rel="icon" type="image/png" href="images/favicon-32x32.png" sizes="32x32" />
  <link rel="icon" type="image/png" href="images/favicon-16x16.png" sizes="16x16" />
  <link href=‘css/typography.css‘ media=‘screen‘ rel=‘stylesheet‘ type=‘text/css‘/>
  <link href=‘css/reset.css‘ media=‘screen‘ rel=‘stylesheet‘ type=‘text/css‘/>
  <link href=‘css/screen.css‘ media=‘screen‘ rel=‘stylesheet‘ type=‘text/css‘/>
  <link href=‘css/reset.css‘ media=‘print‘ rel=‘stylesheet‘ type=‘text/css‘/>
  <link href=‘css/print.css‘ media=‘print‘ rel=‘stylesheet‘ type=‘text/css‘/>
  <script src=‘lib/jquery-1.8.0.min.js‘ type=‘text/javascript‘></script>
  <script src=‘lib/jquery.slideto.min.js‘ type=‘text/javascript‘></script>
  <script src=‘lib/jquery.wiggle.min.js‘ type=‘text/javascript‘></script>
  <script src=‘lib/jquery.ba-bbq.min.js‘ type=‘text/javascript‘></script>
  <script src=‘lib/handlebars-2.0.0.js‘ type=‘text/javascript‘></script>
  <script src=‘lib/js-yaml.min.js‘ type=‘text/javascript‘></script>
  <script src=‘lib/lodash.min.js‘ type=‘text/javascript‘></script>
  <script src=‘lib/backbone-min.js‘ type=‘text/javascript‘></script>
  <script src=‘swagger-ui.js‘ type=‘text/javascript‘></script>
  <script src=‘lib/highlight.7.3.pack.js‘ type=‘text/javascript‘></script>
  <script src=‘lib/jsoneditor.min.js‘ type=‘text/javascript‘></script>
  <script src=‘lib/marked.js‘ type=‘text/javascript‘></script>
  <script src=‘lib/swagger-oauth.js‘ type=‘text/javascript‘></script>

  <script type="text/javascript">
    $(function () {
      var url = window.location.search.match(/url=([^&]+)/);
      if (url && url.length > 1) {
        url = decodeURIComponent(url[1]);
      } else {
        url = "http://localhost:8080/swagger-java-cxf-sample/api/swagger.json";
      }
      window.swaggerUi = new SwaggerUi({
        url: url,
        dom_id: "swagger-ui-container",
        supportedSubmitMethods: [‘get‘, ‘post‘, ‘put‘, ‘delete‘, ‘patch‘],
        onComplete: function(swaggerApi, swaggerUi){
          if(typeof initOAuth == "function") {
            initOAuth({
              clientId: "your-client-id",
              realm: "your-realms",
              appName: "your-app-name"
            });
          }

          $(‘pre code‘).each(function(i, e) {
            hljs.highlightBlock(e)
          });

          addApiKeyAuthorization();
        },
        onFailure: function(data) {
          log("Unable to Load SwaggerUI");
        },
        docExpansion: "none",
        apisSorter: "alpha",
        showRequestHeaders: false
      });

      function addApiKeyAuthorization(){
        var key = encodeURIComponent($(‘#input_apiKey‘)[0].value);
        if(key && key.trim() != "") {
            var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("api_key", key, "query");
            window.swaggerUi.api.clientAuthorizations.add("api_key", apiKeyAuth);
            log("added key " + key);
        }
      }

      $(‘#input_apiKey‘).change(addApiKeyAuthorization);

      // if you have an apiKey you would like to pre-populate on the page for demonstration purposes...
      /*
        var apiKey = "myApiKeyXXXX123456789";
        $(‘#input_apiKey‘).val(apiKey);
      */

      window.swaggerUi.load();

      function log() {
        if (‘console‘ in window) {
          console.log.apply(console, arguments);
        }
      }
  });
  </script>
</head>

<body class="swagger-section">
<div id=‘header‘>
  <div class="swagger-ui-wrap">
    <a id="logo" href="http://swagger.io">swagger</a>
    <form id=‘api_selector‘>
      <div class=‘input‘><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text"/></div>
      <div class=‘input‘><input placeholder="api_key" id="input_apiKey" name="apiKey" type="text"/></div>
      <div class=‘input‘><a id="explore" href="#">Explore</a></div>
    </form>
  </div>
</div>

<div id="message-bar" class="swagger-ui-wrap">&nbsp;</div>
<div id="swagger-ui-container" class="swagger-ui-wrap"></div>
</body>
</html>

把ui相关文件(swagger-ui-master\dist 下的)复制到了webapp下

最后启动项目,访问路径 成功了。

项目下载地址:

[email protected]:witaste/java-jaxrs-cxf-sample.git

时间: 2024-12-31 18:26:25

Spring 、 CXF 整合 swagger 【试炼】的相关文章

Spring boot整合Swagger

本文github位置:https://github.com/WillVi/springboot-swagger2-demo 环境准备 JDK版本:1.8 Spring boot版本:1.5.16 Swagger及其Swagger-ui版本:2.6.1(关于swagger-ui版本 每个版本的汉化方式可能有不同) 默认url:http://localhost:8080/swagger-ui.html Maven依赖 <!-- swagge2 --> <dependency> <

Swagger Learing - Spring Boot 整合swagger

学习了一下swagger. 这是编写的Demo 源码 https://github.com/AmberBar/Learning/tree/master/swagger-learning/swagger 需要的小伙伴可以clone直接运行 访问地址 http://localhost:9999/swagger-ui.html#/ 原文地址:https://www.cnblogs.com/amberbar/p/10308256.html

【Java EE 学习第81天】【CXF框架】【CXF整合Spring】

一.CXF简介 CXF是Apache公司下的项目,CXF=Celtix+Xfire:它支持soap1.1.soap1.2,而且能够和spring进行快速无缝整合. 另外jax-ws是Sun公司发布的一套开发WebService服务的标准.早期的标准如jax-rpc已经很少使用,而cxf就是在新标准jax-ws下开发出来的WebService,jax-ws也内置到了jdk1.6当中. CXF官方下载地址:http://cxf.apache.org/download.html 下载完成之后,解压开压

Apache CXF 整合Spring

一.创建一个 Java Web 工程,目录最终的结构如下图,下面我们将遂一说明: 二.把我们要用到的jar包全部放到lib目录下. 三.修改web.xml文件,整合CXF. <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2e

CXF整合spring(客户端)

在客户端,CXF与spring的整合,实际上是SpringMVC中controller或service调用WebService代理的一个过程,我们只需要将通过wsdl2java生成的文件加入到我们的项目中,然后在Controller或service中依赖注入WebService 的代理,即将生成文件中服务接口注入到Controller中.然后在我们的配置文件中配置生成的服务接口,在这里与我们一般配置bean的方式是不一样的: //这也是配置一个bean <jaxws:client id="

CXF整合Spring开发WebService

刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开环境变量配置窗口,点击新建,新建%CXF_HOME%变量,值为你下载的cxf所在的目录,我的是D:\tools\apache-cxf-3.1.0 2.在Path变量中新加一句%CXF_HOME%\lib,注意要和已有的path变量用;隔开 3.在CLASSPATH中新加一句%CXF_HOME%\li

WebService—CXF整合Spring实现接口发布和调用过程2

一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.18</version> </dependency> <de

spring boot整合cxf发布和调用webservice

一.前言 说起web service最近几年restful大行其道,大有取代传统soap web service的趋势,但是一些特有或相对老旧的系统依然使用了传统的soap web service,例如银行.航空公司的机票查询接口等.本博客主要讲解得是spring boot整合cxf发布webservice服务和spring boot整合cxf客户端调用webservice服务本案例使用maven方式二.编码核心文件清单1.pom.xml <?xml version="1.0"

WebService—CXF整合Spring实现接口发布和调用过程

一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.18</version> </dependency> <de