zno2

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="apiteam@swagger.io"/>
    <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下

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

项目下载地址:

 git@github.com:witaste/java-jaxrs-cxf-sample.git

posted on 2016-08-06 08:17  zno2  阅读(1683)  评论(0编辑  收藏  举报

导航