我正在尝试配置Jersey 1.x REST项目以生成Swagger文档。 其实REST运作良好,但赃物无法运作。 我在WAS 8.5上运行该项目,当我使用浏览器访问URL时:
http://localhost:9082/TestSwagger/swagger.yaml
服务器返回404错误。
我发布了项目中包含的库以及感兴趣的Java文件以寻求帮助:
包含的库:
web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>TestSwagger</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>it.test.application.TestApplication</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.DisableWADL</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
从 javax.ws.rs.core.Application 扩展的类:
package it.test.application;
import io.swagger.jaxrs.config.BeanConfig;
import it.test.rest.impl.TestServiceImpl;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class TestApplication extends Application {
public TestApplication() {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("localhost:9082");
beanConfig.setBasePath("/TestSwagger");
beanConfig.setResourcePackage("it.test.rest.impl");
beanConfig.setScan(true);
}
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(TestServiceImpl.class);
classes.add(io.swagger.jaxrs.listing.ApiListingResource.class);
classes.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);
return classes;
}
}
TestServiceImpl.java ,其中包含REST服务的实现和swagger批注:
package it.test.rest.impl;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Contact;
import io.swagger.annotations.ExternalDocs;
import io.swagger.annotations.Info;
import io.swagger.annotations.License;
import io.swagger.annotations.SwaggerDefinition;
import io.swagger.annotations.Tag;
import it.test.bean.ReturnBean;
import it.test.rest.TestService;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@SwaggerDefinition(
info = @Info(
description = "Test",
version = "1",
title = "Test",
termsOfService = "",
contact = @Contact(
name = "Test",
email = "test@test.it",
url = "http://test"
),
license = @License(
name = "Apache 2.0",
url = "http://www.apache.org/licenses/LICENSE-2.0"
)
),
consumes = {"application/json", "application/xml"},
produces = {"application/json", "application/xml"},
schemes = {SwaggerDefinition.Scheme.HTTP},
tags = {
@Tag(name = "Private", description = "Tag used to denote operations as private")
},
externalDocs = @ExternalDocs(value = "EXT DOC", url = "http://test")
)
@Path("/test/servizi")
public class TestServiceImpl implements TestService {
@Override
@GET
@ApiOperation(value = "Get Hello",
response = TestServiceImpl.class)
@Path("/hello")
@Produces({MediaType.APPLICATION_JSON})
public ReturnBean getHello() throws Exception {
ReturnBean returnBean = new ReturnBean();
returnBean.setRitorno("HELLO");
return returnBean;
}
}
答案 0 :(得分:0)
正如Paul Samsotha所建议的那样,在URL中添加rest
即可!