我正在使用 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)