Ho cercato di usare lo swagger per documentare il servizio jaxrs-resteasy. Voglio configurarlo a livello di codice in un'impostazione del server jetty incorporato. Ecco le mie classi di risorse, server e applicazioni. Posso accedere a http://127.0.0.1:9091/rest/hello
e verificare che il mio servizio di assistenza funzioni ... ma quando provo ad accedere a http://127.0.0.1:9091/rest/api-docs
non riesco a farlo funzionare, ma vedo un 404 non trovato. Qualcuno può indicarmi cosa potrei fare male durante la configurazione di Swagger.Impossibile configurare Swagger con il molo incorporato a livello di codice
file di risorse
@Api(value = "/", description = "Index")
@Path("/")
public class Index {
@GET
@ApiOperation(
value = "call Index",
response = Response.class,
responseContainer = "Object"
)
@Produces("text/html")
public Response index() throws URISyntaxException {
File f = new File(System.getProperty("user.dir")+"/index.html");
String mt = new MimetypesFileTypeMap().getContentType(f);
return Response.ok(f, mt).build();
}
@GET
@Path("/hello")
@ApiOperation(
value = "hello Get",
response = Response.class,
responseContainer = "Object"
)
public Response helloGet() {
return Response.status(200).entity("HTTP GET method called").build();
}
}
Server
public class JettyServer {
public static void main(String[] args) throws IOException {
Server server = new Server(9091);
final HandlerList handlers = new HandlerList();
ServletHolder h = new ServletHolder(new HttpServletDispatcher());
h.setInitParameter("javax.ws.rs.Application", "com.all.MyServices");
// h.setInitOrder(1);
ServletContextHandler restContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
restContextHandler.setContextPath("/rest");
restContextHandler.addServlet(h, "/*");
handlers.addHandler(restContextHandler);
server.setHandler(handlers);
try {
server.start();
server.join();
} catch (Exception e) {
e.printStackTrace();
}
}
}
file di Servizio
public class MyServices extends Application {
private static Set<Index> services = new HashSet<>();
public MyServices() {
System.out.println("Initializing Swagger BeanConfig");
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0");
beanConfig.setResourcePackage(Index.class.getPackage().getName());
beanConfig.setBasePath("http://localhost:9091/api");
beanConfig.setDescription("My RESTful services");
beanConfig.setTitle("My RESTful API");
beanConfig.setScan(true);
services.add(new Index());
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Set getSingletons() {
return services;
}
@SuppressWarnings("rawtypes")
public static Set getServices() {
return services;
}
}
Hai mai trovato una soluzione? Sto avendo un problema simile, ottengo sempre un 404 dove penso che dovrebbero essere le informazioni di Swagger. – medloh
Un'ipotesi: hai configurato correttamente il basepath in beanConfig? Stai tentando di accedere a http: // localhost: 9091/rest/api-docs ma il tuo percorso di base è http: // localhost: 9091/api. Nella mia classe bootstrap per swagger ho anche configurato il percorso API (stesso valore come basePath), nel mio caso: ConfigFactory.config(). SetApiPath ("myPath"); – tareq