0

我正在使用 JBoss AS 7.1.1、RestEasy 2.3.5.Final、Swagger 1.2.0、Spring 3.1.1

这是我的 web.xml,

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/META-INF/spring/application-context.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>Bootstrap</servlet-name>
        <servlet-class>com.js.api.Bootstrap</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/servlet-context.xml</param-value>
        </init-param>
        <init-param>
            <param-name>swagger.config.reader</param-name>
            <param-value>com.js.api.RestEasyConfigReader</param-value>
        </init-param>
        <init-param>
            <param-name>swagger.api.basepath</param-name>
            <param-value>http://localhost:8080/js</param-value>
        </init-param>
        <init-param>
            <param-name>api.version</param-name>
            <param-value>1.0</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

spring application-context & servlet-context.xml 很好,我的 REST 服务已连接并正常工作。但只有招摇是行不通的。这是 ApiListingResource,

package com.js.resource;

import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.springframework.stereotype.Controller;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.jaxrs.listing.ApiListing;

@Path("/api-docs")
@Api("/api-docs")
@Produces({ "application/json" })
@Controller
public class ApiListingResource extends ApiListing {
}

我检查了 @Context 注入在 RestEasy 2.3.5 中不起作用。无论如何创建了一个自定义配置阅读器,

import javax.servlet.ServletConfig;
import com.wordnik.swagger.jaxrs.ConfigReader;

public class RestEasyConfigReader extends ConfigReader {

  private ServletConfig config;

  public RestEasyConfigReader(ServletConfig config){
    this.config =  config;
  }

  @Override
  public String basePath() {
    return getParameterOrDefault("swagger.api.basepath", "http://localhost:8080/js");
  }

  @Override
  public String swaggerVersion() {
    return "1.2";
  }

  @Override
  public String apiVersion() {
    return "1.0";
  }

  @Override
  public String modelPackages() {
    return "com.js.model";
  }

  @Override
  public String apiFilterClassName() {
    return null;
  }

  private String getParameterOrDefault(String key, String defaultValue){
    if ((config != null) && (config.getInitParameter(key) !=null ))
      return config.getInitParameter(key);

    return defaultValue;
  }
}

当我尝试访问时,我总是收到 NPE 错误http://localhost:8080/js/api-docs

java.lang.NullPointerException
    com.wordnik.swagger.jaxrs.ConfigReaderFactory$.getConfigReader(Help.scala:88)
    com.wordnik.swagger.jaxrs.listing.ApiListing.resourceListing(ApiListing.scala:64)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:155)
    org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:257)
    org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:222)
    org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:211)
    org.jboss.resteasy.springmvc.ResteasyHandlerAdapter.createModelAndView(ResteasyHandlerAdapter.java:87)
    org.jboss.resteasy.springmvc.ResteasyHandlerAdapter.handle(ResteasyHandlerAdapter.java:74)
    org.jboss.resteasy.springmvc.ResteasyHandlerAdapter.handle(ResteasyHandlerAdapter.java:24)
    org.jboss.resteasy.springmvc.ResteasyWebHandlerTemplate.handle(ResteasyWebHandlerTemplate.java:39)
    org.jboss.resteasy.springmvc.ResteasyHandlerAdapter.handle(ResteasyHandlerAdapter.java:45)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
4

3 回答 3

2

不仅@Context 注入有效。Swagger 依赖 Application & ServletConfig,Spring 没有在方法参数中注入。

我决定最好编写代码来解决这个问题。

  1. 配置由 Spring bean 读取。它可以在spring xml中配置。
  2. 编写 REST 资源以公开文档。
  3. REST 类是通过扫描所有类找到的。使用了如何从类路径中的 Java 包中读取所有类的解决方案?.
  4. 类的 API 文档是通过调用 Swagger-Jaxrs 代码形成的

    JaxrsApiReader.read(clazz, apiVersion, swaggerVersion, basePath, apiPath)

于 2013-03-07T06:54:48.727 回答
1

我能够连接 swagger 1.3.0、resteasy、spring 3.x 和 jboss-as-7。

你可以在spring.xml中配置swagger相关的bean,如下:

 <!-- Swagger providers -->
   <bean id="apiDeclarationProvider" class="com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider" />
   <bean id="resourceListingProvider" class="com.wordnik.swagger.jaxrs.listing.ResourceListingProvider" />

<!-- Swagger API listing resource -->
   <bean id="swaggerResourceJSON" class="com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON" />

<!-- this scans the classes for resources -->
   <bean id="swaggerConfig" class="com.wordnik.swagger.jaxrs.config.BeanConfig">
     <property name="resourcePackage" value="package.name"/>
     <property name="version" value="1.0.0"/>
     <property name="basePath" value="http://localhost:8081/resteasy-spring"/>
     <property name="title" value="Petstore sample app"/>
     <property name="description" value="This is a app."/>
     <property name="contact" value="apiteam@wordnik.com"/>
     <property name="license" value="Apache 2.0 License"/>
     <property name="licenseUrl" value="http://www.apache.org/licenses/LICENSE-2.0.html"/>
     <property name="scan" value="true"/>
   </bean>

然后您可以在 web.xml 中将 swagger 配置为:

<servlet>
     <servlet-name>DefaultJaxrsConfig</servlet-name>
     <servlet-class>com.wordnik.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
     <init-param>
         <param-name>api.version</param-name>
         <param-value>1.0.0</param-value>
     </init-param>
     <load-on-startup>2</load-on-startup>
</servlet>

欲了解更多详情:请访问此处

于 2014-01-03T14:24:52.657 回答
0

最近,我们将 Spring 应用程序从 tomcat 转移到了 wildfly(JBoos 8)。在那之后,招摇并没有奏效。以前,我们在 web.xml 中提到了everytinh,它在 tomcat 7 上运行良好。更改为 wildfly 后,我们做了以下更改:

我们从 web.xml 中删除了以下内容:

<servlet>
        <servlet-name>DefaultJaxrsConfig</servlet-name>
        <servlet-class>com.wordnik.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
        <init-param>
            <param-name>api.version</param-name>
            <param-value>1.0.0</param-value>
        </init-param>
        <init-param>
            <param-name>swagger.api.basepath</param-name>
            <param-value>http://localhost:8080/rest/api</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>

在 spring-context.xml 中添加以下内容:

<!-- Swagger providers -->
    <bean id="apiDeclarationProvider"
        class="com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider" scope="singleton" />
    <bean id="resourceListingProvider"
        class="com.wordnik.swagger.jaxrs.listing.ResourceListingProvider"  scope="singleton"/>

    <!-- Swagger API listing resource -->
    <bean id="swaggerResourceJSON"
        class="com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON" />

<!-- this scans the classes for resources -->
    <bean id="swaggerConfig" class="com.wordnik.swagger.jaxrs.config.BeanConfig">
        <property name="resourcePackage" value="com.rest" />
        <property name="version" value="1.0.0" />
        <property name="basePath" value="https://localhost:9880/rest/api" />
        <property name="title" value="REST API" />
        <property name="scan" value="true" />
    </bean>

注意:您需要定义 scope=singleton 否则 wildfly 会抛出异常。

现在,重新启动您的服务器,并加载您的 URL。它应该工作。

于 2014-04-25T18:19:05.413 回答