我正在開發一個REST API,並試圖使用Swagger和澤西一起做這件事。
我遵循這裏給出的指導: - https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5
我使用ResourceConfig
而不是Application
類,並相應地修改了指南中的步驟。我在灰熊上部署API。如何在運動衫中使用Swagger和ResourceConfig?
這裏,是主文件: -
package com.example;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import java.io.IOException;
import java.net.URI;
import io.swagger.jaxrs.config.*;
/**
* Main class.
*
*/
public class Main {
// Base URI the Grizzly HTTP server will listen on
public static final String BASE_URI = "http://localhost:8080/";
/**
* Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
* @return Grizzly HTTP server.
*/
public static HttpServer startServer() {
// create a resource config that scans for JAX-RS resources and providers
// in com.example package
final ResourceConfig rc = new ResourceConfig().packages("com.example");
rc.register(io.swagger.jaxrs.listing.ApiListingResource.class);
rc.register(io.swagger.jaxrs.listing.SwaggerSerializers.class);
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.2");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("localhost:8080");
beanConfig.setBasePath("resources/api");
beanConfig.setResourcePackage("io.swagger.resources");
beanConfig.setScan(true);
// create and start a new instance of grizzly http server
// exposing the Jersey application at BASE_URI
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}
/**
* Main method.
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
final HttpServer server = startServer();
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
System.in.read();
server.stop();
}
}
現在,當我嘗試在URL http://localhost:8080/swagger.json
訪問swagger.json,我得到一個500,請求失敗的錯誤。
這是爲什麼,我該如何調試它?
查看本帖[](http://stackoverflow.com/q/33681371/2587435)。可能相關 –
@peeskillet謝謝!這是同樣的問題。 –