简体   繁体   English

使用Jersey 2时,Tomcat 7不会扫描注释

[英]Tomcat 7 does not scan annotations when using Jersey 2

I have a Jersey 2 Maven project. 我有一个Jersey 2 Maven项目。

web.xml: web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" 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_2_5.xsd">
    <servlet>
        <servlet-name>com.api.rest.ApplicationResource</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>        
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>com.api.rest.ApplicationResource</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>
</web-app>

pom.xml : pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.api</groupId>
    <artifactId>GLI_API</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>GLI_API</name>

    <build>
        <finalName>GLI_API</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>2.21</version>
        </dependency>


        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP-java6</artifactId>
            <version>2.2.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>org.mindrot</groupId>
            <artifactId>jbcrypt</artifactId>
            <version>0.3m</version>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
        <dependency>
            <groupId>com.cloudinary</groupId>
            <artifactId>cloudinary-http44</artifactId>
            <version>1.2.1</version>
        </dependency>
    </dependencies>
    <properties>
        <jersey.version>2.19</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

My project works fine when working locally. 我的项目在本地工作时工作正常。 But it doesn't work anymore when i deploy it on my remote server. 但是当我在远程服务器上部署它时,它不再起作用了。

So what happens is basically, that the WAR gets deployed correctly but there is really no error or warning in catalina.out. 所以基本上会发生什么,WAR正确部署,但catalina.out中确实没有错误或警告。 I get a 404 on every of my defined endpoints. 我在每个定义的端点上得到404。

catalina.out : catalina.out

Sep 14, 2015 2:03:00 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /var/lib/tomcat7/webapps/GLI_API.war
Sep 14, 2015 2:04:17 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /var/lib/tomcat7/webapps/ICDS_API.war
Sep 14, 2015 2:04:23 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /var/lib/tomcat7/webapps/ROOT
Sep 14, 2015 2:04:23 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Sep 14, 2015 2:04:23 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 84867 ms

ApplicationResource : ApplicationResource

package com.api.rest;

import java.util.logging.Logger;


import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import com.api.provider.ResponseCorsFilter;
import org.glassfish.jersey.media.multipart.MultiPartFeature;


public class ApplicationResource extends ResourceConfig {

    private static final Logger LOGGER = null;   


    public ApplicationResource() {
        // Register resources and providers using package-scanning.
        packages("com.api");
        System.out.println("started");

        register(MultiPartFeature.class);

        // Register my custom provider - not needed if it's in my.package.
        register(ResponseCorsFilter.class);
        // Register an instance of LoggingFilter.
        register(new LoggingFilter(LOGGER, true));

        // Enable Tracing support.
        property(ServerProperties.TRACING, "ALL");
    }
}

What could be the problem? 可能是什么问题呢? I developed on tomcat8 (but don't remember using something specifically) and deployed on tomcat7. 我在tomcat8上开发(但不记得特别使用了什么)并部署在tomcat7上。 Could this be a problem? 这可能是个问题吗?


This endpoint should be reachable: http://54.228.220.152:8080/GLI_API/api/item 应该可以访问此端点:http: //54.228.220.1528080 / GLI_API/api/item

The other API works fine - so the server in general is working. 另一个API工作正常 - 所以服务器一般都在工作。

Also - if you don't have the final answer i would really appreciate it if you can point me to files/directories to look at since i don't get any error. 此外 - 如果你没有最终的答案,我真的很感激,如果你可以指向我看文件/目录,因为我没有得到任何错误。

You may try to enable annotation scanning by setting this init parameter to your servlet: 您可以尝试通过将此init参数设置为servlet来启用注释扫描:

<servlet>
 ...
 <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

 <init-param>
  <param-name>jersey.config.server.provider.packages</param-name>
  <param-value>com.yourproject.packageWhereYourResourcesAre</param-value>
 </init-param>
...
</servlet>

Update 更新

It seems you have compiled against Java 8, while Tomcat runs with Java 7? 看来你已经编译了Java 8,而Tomcat是用Java 7运行的? Try this: 尝试这个:

<configuration>
    <source>1.7</source>
    <target>1.7</target>
</configuration>

You may also want to update your web.xml to 3.0: 您可能还想将web.xml更新为3.0:

<web-app 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">

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

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