问题
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 .
- How can I get json separately for each endpoint?
- Can i get them generated locally in a file system?
- The big json file loads the UI at once. I would like to have separate links for each endpoint type.
回答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 behttp://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. - 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.
- 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