我正在努力工作来记录使用RestEasy创建的API。
我尽可能地遵循可用的文档here。
我有:
<dependency> <groupId>io.swagger</groupId> <artifactId>swagger-jaxrs</artifactId> <version>1.5.0</version> </dependency>
<context-param> <param-name>resteasy.scan</param-name> <param-value>true</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>1.0.0</param-value> </init-param> <init-param> <param-name>swagger.api.basepath</param-name> <param-value>http://localhost:8080/mygiftlist/rest</param-value> </init-param> <init-param> <param-name>swagger.api.resourcePackage</param-name> <param-value>com.jetnuts.rest</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet>
我在
下有几个工作端点localhost:8080/mygiftlist/rest
全部由&#34; com.jetnuts.rest&#34;设置。封装
但问题是,当我访问时:
http://localhost:8080/mygiftlist/rest/swagger.json
我得到的只是一个空API:
{"swagger":"2.0","info":{"version":"1.0.0","title":""},"host":"localhost:8080","basePath":"/mygiftlist/rest","schemes":["http"]}
有谁知道为什么这不会在包中拿起端点?我测试了端点,它们都有效。
例如,我有(com.jetnuts.rest):
/**
* Created by steve on 13/12/2015.
*/
@Path("/giftlists")
@RequestScoped
public class GiftListResourceRESTService {
@Inject
CrudService dao;
/**
* Create a new list
* @param giftList The gift list to create
* @return A created response, or an error if the incorrect format is given.
*/
@POST
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
public Response createGift(GiftList giftList) {
dao.create(giftList);
try {
URI resourceURI = new URI("/giftlists/" + giftList.getId());
return Response.created(resourceURI).build();
}catch(URISyntaxException e){
return Response.serverError().build();
}
}
}
此端点可以正常工作并按预期添加到数据库中,但它并没有显示在swagger json中。
为了完整性,我的应用程序&#34; class看起来像这样:
@ApplicationPath("/rest")
public class JaxRsActivator extends Application {
public JaxRsActivator(){
}
}
最后它有效,但我不知道为什么?
将servlet移出web.xml并将其添加到&#34;应用程序&#34;后,它可以工作。我仍然不知道为什么会这样:
public JaxRsActivator(){
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.2");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("localhost:8080");
beanConfig.setBasePath("/rest/");
beanConfig.setResourcePackage("com.jetnuts.rest");
beanConfig.setScan(true);
}
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new HashSet();
resources.add(io.swagger.jaxrs.listing.ApiListingResource.class);
resources.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);
return resources;
}