How to get swagger json separately?

∥☆過路亽.° 提交于 2019-12-12 05:44:27

问题


I try to generate swagger.json from my java rest endpoints.

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();
}

...

When i deploy my testdbwar in wildfly, I try to access my json like; http://localhost:8080/testdbwar/rest/swagger.json?

And i get a big json which contains all jsons .

  1. How can I get json separately for each endpoint?
  2. Can i get them generated locally in a file system?
  3. The big json file loads the UI at once. I would like to have separate links for each endpoint type.

回答1:


  1. Generally for any swagger library, it is all_resource_listing_url/pathName. When I googled for jax-rs, this is one I found somewhat related to your problem though that was with wordnik.swagger library. In your case, it should be http://localhost:8080/testdbwar/rest/swagger.json/Addresss for apis listed under path 'Addresss'. I don't know, whether you misspelled 'Addresss' with 'sss' and tried with 'ss' already. And please also try with a lower-case path name.
  2. I don't think there's an automated way with swagger configuration itself. But You can do that manually with a small-code right? Anyway It looks like, that kind of a support is available there with some commercial tools like IBM.
  3. A solution for Q1 is applicable to this one too, right? Inside the swagger-ui, you can invoke the swagger doc urls differently for each different paths.


来源:https://stackoverflow.com/questions/37427988/how-to-get-swagger-json-separately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!