swagger - 没有API的空列表 [英] swagger - empty listing with no API
问题描述
我正在尝试使用swagger来记录我的Rest API。我正在开发一个Tomcat / Spring服务器,其余的api是使用Jersey开发的。
I am trying to use swagger in order to document my Rest APIs. I am developing a Tomcat/Spring server and the rest apis are developed using Jersey.
我按照swagger指南并将所需数据添加到我的web.xml:
I following swagger guide and added the required data to my web.xml:
<servlet>
<servlet-name>resources</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.wordnik.swagger.jersey.listing;app.servlet.resources.jersey;org.codehaus.jackson.jaxrs</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://localhost:8080/main/resources/</param-value>
</init-param>
<init-param>
<param-name>api.version</param-name>
<param-value>1</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
http://:8080 / main / resources / api-docs返回以下内容:
http://:8080/main/resources/api-docs returns the following:
{"apiVersion":"0.0","swaggerVersion":"1.2"}
我知道我在这里什么?
推荐答案
尝试将以下内容添加到web.xml(确保定义相应的 servlet-class
位置):
Try to add the following to your "web.xml" (make sure you define the appropriate servlet-class
locations):
<servlet>
<servlet-name>Bootstrap</servlet-name>
<servlet-class>com.mywebservice.utils.swagger.Bootstrap</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<filter>
<filter-name>ApiOriginFilter</filter-name>
<filter-class>com.mywebservice.utils.swagger.ApiOriginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ApiOriginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
并确保你有Bootstrap,ApiOriginFilter,ApiListingResource以及ApiListingResourceJSON (扩展了ApiListing)。请参阅以下这些文件的示例:
and make sure you have "Bootstrap", "ApiOriginFilter", "ApiListingResource" and also "ApiListingResourceJSON" (which extends ApiListing). See in the following examples of these files:
Bootstrap:
Bootstrap:
/**
* Copyright 2012 Wordnik, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import com.wordnik.swagger.jaxrs.JaxrsApiReader;
import javax.servlet.http.HttpServlet;
public class Bootstrap extends HttpServlet{
private static final long serialVersionUID = 1L;
static{
JaxrsApiReader.setFormatString("");
}
}
ApiOriginFilter:
ApiOriginFilter:
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
public class ApiOriginFilter implements javax.servlet.Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException{
HttpServletResponse res = (HttpServletResponse) response;
res.addHeader("Access-Control-Allow-Origin", "*");
res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
res.addHeader("Access-Control-Allow-Headers", "Content-Type");
chain.doFilter(request, response);
}
@Override
public void destroy(){
}
@Override
public void init(FilterConfig filterConfig) throws ServletException{
}
}
ApiListingResource:
ApiListingResource:
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.jaxrs.JavaApiListing;
@Path("/resources.json")
@Api("/resources")
@Produces({ "application/json"})
public class ApiListingResource extends JavaApiListing{
}
ApiListingResourceJSON:
ApiListingResourceJSON:
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.jaxrs.listing.ApiListing;
@Path("/api-docs")
@Api("/api-docs")
@Produces({ "application/json"})
public class ApiListingResourceJSON extends ApiListing{
}
这应该是...... Swagger是非常好,一旦你有它设置它只是工作,但要设置它需要做一些体操仍然。
This should be it... Swagger is very nice, and once you have it set up it "just works", but to set it up one needs to do some gymnastics still.
HTH。
这篇关于swagger - 没有API的空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!