2016-05-31 462 views
1

我正在嘗試使用Swagger的API文檔來處理我的'org.jboss.resteasy'Rest服務。配置完成後,我可以正確訪問'http://localhost:8080/myrestswagger/rest/swagger.json'。 返回以下內容:/swagger.json正在工作,但無法訪問/ api-docs?

{ 
    "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> 

回答

0

你不需要兩個swagger.jsonapi-docs。兩者都是通用名稱,用於描述服務器中的Swagger定義,Swagger UI可以使用它作爲URL來呈現API的交互式視圖。

從查看swagger.json的輸出,看起來您並未掃描您的資源。請參閱關於將您的CountryController添加到API的掃描路徑。

+0

'<的context-param> ​​resteasy.providers io.swagger.jaxrs.listing.ApiListingResource, io.swagger.jaxrs.listing.SwaggerSerializers, com.springrest.rest.CountryController '但無法加載'/ countryDetails'的文檔數據 – Sha

相關問題