2016-05-25 23 views
0

我嘗試從我的Java休息端點生成swagger.json。如何分開獲得swagger json?

POM

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

Applicationpath

import javax.ws.rs.ApplicationPath; 

@ApplicationPath("/rest") 
public class RestApplication extends Application { 
    public RestApplication() { 
     BeanConfig beanConfig = new BeanConfig(); 
     //beanConfig.setVersion("1.0"); 
     beanConfig.setSchemes(new String[] { "http" }); 
     beanConfig.setTitle("My API"); 
     beanConfig.setBasePath("/rest"); 
     beanConfig.setResourcePackage("com.test.rest"); 
     beanConfig.setScan(true); 
    } 

    @Override 
    public Set<Class<?>> getClasses() { 
     HashSet<Class<?>> set = new HashSet<Class<?>>(); 

     set.add(com.test.AddressEndpoint.class); 
     set.add(com.test.ATGEndpoint.class); 
     set.add(com.test.CompanyEndpoint.class) 

     set.add(io.swagger.jaxrs.listing.ApiListingResource.class); 
     set.add(io.swagger.jaxrs.listing.SwaggerSerializers.class); 

     return set; 
    } 
} 

AddressEndpoint

@Api 
@Singleton 
@Path("/Addresss") 
@Produces("application/json") 
@ConcurrencyManagement 
@Lock(LockType.READ) 
public class AddressEndpoint { 

private static final Logger log = Logger.getLogger(AddressEndpoint.class.getName()); 

@Context 
private HttpRequest httpRequest; 

@Inject 
AddressService AddressService; 
/** 
* @description creates a new Address 
* @status 400 Address country cannot be null 
* @status 400 Address address2 cannot be null 
* @status 400 Address city cannot be null 
* @status 400 Address address1 cannot be null 
* @status 400 Address postcode cannot be null 
* @status 400 Address id cannot be null 
* @status 400 Address state cannot be null 
* @status 201 Address created successfully 
*/ 
@POST 
@Consumes("application/json") 
public Response create(Address entity) { 
    AddressService.insert(entity); 
    return Response.created(UriBuilder.fromResource(AddressEndpoint.class).path(String.valueOf(entity.getId())).build()).build(); 
} 

...

當我在wildfly中部署我的testdbwar時,我嘗試訪問我的json; http://localhost:8080/testdbwar/rest/swagger.json?

而且我得到一個包含所有jsons的大json。

  1. 如何爲每個端點分別獲取json?
  2. 我可以讓它們在文件系統中本地生成嗎?
  3. 大JSON文件一次加載UI。我想要爲每個端點類型分開鏈接。
+0

什麼是您使用的Java框架?或者你可以發佈任何鏈接,如果有的話,你遵循配置Swagger? –

+0

@SweetWijerathne io.swagger圖書館 – Ratha

+0

不,我問的是應用程序框架,例如:Spring MVC,Play等? –

回答

0
  1. 通常對於任何招搖的庫,它是all_resource_listing_url/pathName。當我爲jax-rs搜索Google時,this是我發現與你的問題有點相關的一個,雖然這是與wordnik.swagger庫。在你的情況下,對於在路徑'Addresss'下列出的apis,應該是http://localhost:8080/testdbwar/rest/swagger.json/Addresss。我不知道,你是否用'sss'拼寫'Addresss'並用'ss'嘗試過。並且還請嘗試使用小寫路徑名稱。
  2. 我不認爲有一個自動的方式與swagger配置本身。但是你可以用小代碼手動執行該操作嗎?無論如何,它看起來像那種支持可用於一些商業工具,如IBM
  3. Q1的解決方案也適用於這一個,對嗎?在swagger-ui內部,你可以針對每個不同的路徑調用swagger doc url。