【问题标题】:/swagger.json is working but cannot access /api-docs?/swagger.json 正在工作但无法访问 /api-docs?
【发布时间】:2016-09-29 13:05:22
【问题描述】:

我正在尝试通过 Swagger 将 API 文档用于我的“org.jboss.resteasy”Rest 服务。配置后我可以正确访问'http://localhost:8080/myrestswagger/rest/swagger.json'。 它返回以下内容:

{
  "swagger": "2.0",
  "info": {
    "version": "3.0.0",
    "title": ""
  },
  "host": "localhost:8080",
  "basePath": "/myrestswagger/rest",
  "schemes": [
    "http"
  ]
}

但我无法访问或生成关于“http://localhost:8080/myrestswagger/rest/api-docs”的任何数据,请查看我的课程。

休息服务类:

@Path("/countryDetails")
@Api( value = "/countryDetails", description = "countryDetails" )
public class CountryController {

@Path("/countries")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "GetCountries", httpMethod = "GET", notes = "Get Countries against Specific URL", response = Country.class)
public List<Country> getCountries() {
    List<Country> listOfCountries = new ArrayList<Country>();
    listOfCountries = createCountryList();
    return listOfCountries;
}

@Path("/country/{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Country getCountryById(@PathParam("id") int id) {
    List<Country> listOfCountries = new ArrayList<Country>();
    listOfCountries = createCountryList();
    for (Country country : listOfCountries) {
        if (country.getId() == id) return country;
    }
    return null;
}

private List<Country> createCountryList() {
    Country indiaCountry = new Country(1, "India");
    Country chinaCountry = new Country(4, "China");
    Country nepalCountry = new Country(3, "Nepal");
    Country bhutanCountry = new Country(2, "Bhutan");
    List<Country> listOfCountries = new ArrayList<Country>();
    listOfCountries.add(indiaCountry);
    listOfCountries.add(chinaCountry);
    listOfCountries.add(nepalCountry);
    listOfCountries.add(bhutanCountry);
    return listOfCountries;
}
}

这是 web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     id="WebApp_ID" version="3.0">

<listener>
    <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
    <servlet-name>Resteasy</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Resteasy</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>io.undertow.servlet.handlers.DefaultServlet</servlet-class>
    <init-param>
        <param-name>allowed-extensions</param-name>
        <param-value>js, css, png, jpg, gif, html, htm, txt, pdf, jpeg, xml, zip, jar</param-value>
    </init-param>
    <init-param>
        <param-name>disallowed-extensions</param-name>
        <param-value>class, war</param-value>
    </init-param>
</servlet>
<context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/rest</param-value>
</context-param>
<!--While using Spring integration set resteasy.scan to false or don't configure resteasy.scan parameter at all -->
<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>resteasy.providers</param-name>
    <param-value>
        io.swagger.jaxrs.listing.ApiListingResource,
        io.swagger.jaxrs.listing.SwaggerSerializers
    </param-value>
</context-param>
<servlet>
    <servlet-name>Jersey2Config</servlet-name>
    <servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
    <init-param>
        <param-name>api.version</param-name>
        <param-value>3.0.0</param-value>
    </init-param>
    <init-param>
        <param-name>swagger.api.basepath</param-name>
        <param-value>http://localhost:8080/myrestswagger/rest</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

这是我的 pom.xml 依赖项(maven.project)

<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-jaxrs</artifactId>
    <version>1.5.9</version>
</dependency>

【问题讨论】:

    标签: java rest swagger


    【解决方案1】:

    您不需要swagger.jsonapi-docs。两者都是描述服务器中 Swagger 定义的常用名称,Swagger UI 可以将其用作 URL 来呈现 API 的交互式视图。

    从您的swagger.json 的输出看,您似乎没有在扫描您的资源。请参阅关于将您的 CountryController 添加到 API 的扫描路径。

    【讨论】:

    • &lt;context-param&gt; &lt;param-name&gt;resteasy.providers&lt;/param-name&gt; &lt;param-value&gt; io.swagger.jaxrs.listing.ApiListingResource, io.swagger.jaxrs.listing.SwaggerSerializers, com.springrest.rest.CountryController &lt;/param-value&gt; &lt;/context-param&gt; 但无法加载“/countryDetails”的文档数据
    猜你喜欢
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多