我有一个现有的带有REST API的maven项目。我正在尝试将Swagger与项目集成。当我运行项目时,我得到了空的Swagger UI页面。没有加载API。我哪里错了?
的pom.xml
<!-- Swagger -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>1.5.16</version>
</dependency>
我使用的是自定义Application类(不是web.xml)
public class MyRestApplication extends ResourceConfig {
public MyRestApplication() {
System.out.println("Entering Custom Application");
property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
property(ServerProperties.
BV_DISABLE_VALIDATE_ON_EXECUTABLE_OVERRIDE_CHECK, true);
register(RolesAllowedDynamicFeature.class);
register(StorageResource.class);
register(io.swagger.jaxrs.listing.ApiListingResource.class);
register(io.swagger.jaxrs.listing.SwaggerSerializers.class);
}
}
在我的StorageResource文件中:
@Api(value = "/Person")
@Path("")
public class StorageResource {
static Logger logger =
Logger.getLogger(StorageResource.class.getName());
@PUT
@Path("/person-manager-resource/addPerson")
@Produces("application/json")
@Consumes("application/json")
@ApiOperation(
value = "method api",
notes = "method api notes"
)
public Object addPerson(String reqBody) {
AddPerson add = new AddPerson();
return add.addPerson(reqBody);
}
}
在Swagger UI的index.html中(从 dist 文件夹复制到webapp文件夹)
window.onload = function() {
// Build a system
const ui = SwaggerUIBundle({
url: "http://localhost:8080/demographics/dgs/swagger.json",
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
window.ui = ui
}
我的web.xml文件:
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>in.healthelife.DGS</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>in.healthelife.DGS.data.MyRestApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/dgs/*</url-pattern>
</servlet-mapping>
答案 0 :(得分:0)
<强> UPDATE2 强>
我不明白为什么你使用了2 org.glassfish.jersey.servlet.ServletContainer
它不起作用
确保您提到您的自定义应用程序是通过在web.xml
中提及的泽西容器加载的如下所示
swagger-jersey2-jaxrs
依赖将足够休息将被传递。我不知道您正在使用哪种版本的球衣,但您必须延长ResourceConfig
才能自定义应用程序Application
课程本身
您必须在扩展资源配置中注册这两项功能
io.swagger.jaxrs.listing.ApiListingResource,
io.swagger.jaxrs.listing.SwaggerSerializers
您的自定义应用程序类必须在您的web.xml中提及,如此
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.foo.app.CustomApplication
</param-value>
</init-param>
</servlet>
<强>更新强>
url: "http://localhost:8080/OpenEMPIStorage/rest/api-docs",
最好的是这是swagger.json的相对路径所以它应该是这样的
url: "swagger.json"
并确保您在路径http://localhost:8080/OpenEMPIStorage/rest/api-docs
中进入的json被复制到此swagger.json文件中
并且您在Resource Config中的代码应该像
public MyRestApplication()
{
System.out.println("Entering Custom Application");
property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
// @ValidateOnExecution annotations on subclasses won't cause errors.
property(ServerProperties.BV_DISABLE_VALIDATE_ON_EXECUTABLE_OVERRIDE_CHECK, true);
register(RolesAllowedDynamicFeature.class);
register(AuthenticationFilter.class);
register(AuthenticationResponseFilter.class);
}
请勿使用@ApplicationPath
你的Swagger.json应该在fallowing路径下可用
http://<ip address:portname or domain name >/<yourapp-path>/dgs/swagger.json