Jersey2Config上的ClassNotFoundException(io.swagger.jersey.config.JerseyJaxrsConfig)

时间:2015-10-14 14:23:37

标签: swagger swagger-ui

我已经为swagger配置了我的项目,我的项目是maven,struts,hibernate。我在pom中添加了依赖

<!-- Swagger -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-jersey2-jaxrs</artifactId>
            <version>1.5.2</version>
            <exclusions>
                <exclusion>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-databind</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-annotations</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

和web.xml是这样的:

<servlet>
        <servlet-name>Jersey2Config</servlet-name>
        <servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
         <init-param>
            <param-name>api.version</param-name>
            <param-value>1.0.0</param-value>
        </init-param>
        <init-param>
            <param-name>swagger.api.basepath</param-name>
            <param-value>/PatientApp/services/</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

还添加了核心过滤器:

<filter>
        <filter-name>CorsFilter</filter-name>
        <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CorsFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

但是当我尝试运行该项目时,它给了我错误。堆栈跟踪在同伴之下:

Oct 14, 2015 5:40:18 PM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet Jersey2Config as unavailable
Oct 14, 2015 5:40:18 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /PatientApp threw load() exception
java.lang.ClassNotFoundException: io.swagger.jersey.config.JerseyJaxrsConfig

2 个答案:

答案 0 :(得分:0)

可以查看以下参数是否正确

  1. 列出项目

    <init-param>
        <param-name>swagger.api.basepath</param-name>
        <param-value>/PatientApp/services/</param-value>
    </init-param>
    
  2. 通常就是这样                      swagger.api.basepath             http://localhost:8080/api         

    • 你在web.xml中有servlet映射吗? 2
    • 更改jar的版本,试试这个   com.wordnik   招摇汗布,jaxrs_2.10   1.3.12

答案 1 :(得分:0)

您还没有分享web.xml中其他条目的外观。另外我在pom.xml中没有任何排除。您可以从下面的选项2查看我的web.xml,看看您的配置有何不同。

有两种方法可以配置JerseyJaxrsConfig

1。在web.xml里面

<servlet>
    <servlet-name>Jersey2Config</servlet-name>
    <servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
    <init-param>
        <param-name>api.version</param-name>
        <param-value>1.0.0</param-value>
    </init-param>
    <init-param>
        <param-name>swagger.api.basepath</param-name>
        <param-value>http://localhost:8080/jax_rs/rest</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

2。在自定义JaxrsConfig类

第二种方法是创建一个Config Java类并从web.xml指向它。您可以在此处详细阅读:https://stackoverflow.com/a/35905415/5076414