简体   繁体   English

swagger ui与maven项目的集成不显示API

[英]swagger ui integration with maven project not displaying APIs

I have an existing maven project with REST APIs. 我有一个带有REST API的现有Maven项目。 I am trying to integrate Swagger with the project. 我正在尝试将Swagger与该项目集成。 When I run the project, I get the empty Swagger UI page. 运行项目时,我得到了一个空的Swagger UI页面。 No APIs are loaded. 未加载任何API。 Where am I going wrong? 我要去哪里错了?

pom.xml 的pom.xml

<!-- Swagger -->
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-jersey2-jaxrs</artifactId>
        <version>1.5.16</version>
    </dependency>

I am using a custom Application class(not web.xml) 我正在使用自定义应用程序类(不是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);        
}
}    

In my StorageResource file: 在我的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);
}
}

In the index.html of Swagger UI(copied from dist folder to webapp folder) 在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
}

My web.xml file: 我的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>

update2 UPDATE2
I dont understand why you have used 2 org.glassfish.jersey.servlet.ServletContainer it will not work 我不明白为什么您使用了2 org.glassfish.jersey.servlet.ServletContainer它不起作用

make sure your are mentioning your custom application is loaded through jersey container by mentioning it in web.xml 通过在web.xml中提及来确保您提到的自定义应用程序是通过jersey容器加载的

like shown below 如下图所示


swagger-jersey2-jaxrs

dependency will be enough rest will be pulled transitively . 依赖将足够,其余将被暂时取消。 i don't know which version of jersey you are using but you have to extend ResourceConfig in order to customize the application not Application class itself 我不知道您使用的是哪个版本的球衣,但是您必须扩展ResourceConfig才能自定义应用程序,而不是自定义应用Application类本身

You have to register these 2 features in your extended resource config 您必须在扩展资源配置中注册这两个功能

io.swagger.jaxrs.listing.ApiListingResource,
     io.swagger.jaxrs.listing.SwaggerSerializers

And your custom application class has to be mentioned in your web.xml like so 像这样,必须在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>

Update 更新

url: "http://localhost:8080/OpenEMPIStorage/rest/api-docs",

its best that this is relative path of swagger.json so it should be like this 最好是这是swagger.json的相对路径,所以应该像这样

 url: "swagger.json"

and make sure the json you are getting in in path http://localhost:8080/OpenEMPIStorage/rest/api-docs is copied in this swagger.json file 并确保将您在路径http://localhost:8080/OpenEMPIStorage/rest/api-docs中获取的json复制到此swagger.json文件中

and your code inside Resource Config should be like 并且您在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);
 }

don't use @ApplicationPath 不要使用@ApplicationPath

your Swagger.json should be available under fallowing path 您的Swagger.json应该在休闲路径下可用

http://<ip address:portname or domain name >/<yourapp-path>/dgs/swagger.json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM