Jersey应用程序404未找到错误

时间:2014-04-19 18:26:23

标签: java json rest maven jersey

我正在使用泽西开发一个应用程序,但我找不到404异常

这是web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">


    <display-name>Passport parking</display-name>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

   <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>
                     org.glassfish.jersey.servlet.ServletContainer
                </servlet-class>
        <init-param>
             <param-name>com.sun.jersey.config.property.packages</param-name>
             <param-value>com.passportparking.webservice.resource</param-value>
        </init-param>
                 <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>        
    </init-param>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.passportparking.webservice.modal.CalculatorJerseyApplication</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>


    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>/index.html</welcome-file>
    </welcome-file-list>
</web-app>

Calculator.java资源类

package com.passportparking.webservice.resource;



import com.passportparking.webservice.DAO.CalculatorDAO;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import com.passportparking.webservice.modal.CalculatorModal;
import com.passportparking.webservice.modal.WebServiceStatus;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;


@Path("/calculator/calculate")
public class Calculator {


    @Context UriInfo uriInfo;
    @Context Request request;


    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public WebServiceStatus calculate(@Context HttpServletRequest req, CalculatorModal modal) {
        return CalculatorDAO.doProcess(modal);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public WebServiceStatus serviceStatus() {
        System.out.println("get method");
        return WebServiceStatus.success("Service running properly");
    }


}

日志

INFO:   Initiating Jersey application, version Jersey: 2.0 2013-05-03 14:50:15...
INFO:   Loading application [passportparking] at [/passportparking]
INFO:   passportparking was successfully deployed in 42,555 milliseconds.
INFO:   1 * LoggingFilter - Request received on thread http-listener-1(3)
1 > GET http://localhost:8080/passportparking/rest/calculator/calculate
1 > host: localhost:8080
1 > user-agent: Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0
1 > accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
1 > accept-language: en-US,en;q=0.5
1 > accept-encoding: gzip, deflate
1 > connection: keep-alive

INFO:   1 * LoggingFilter - Response received on thread http-listener-1(3)
1 < 404

我想使用jersey pojo支持,以便我的webservice状态类将转换为json,json将转换为CalculatorModal

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>passportparking</groupId>
    <artifactId>passportparking</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>passportparking</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.2.7.SP1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.27</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.2.7.SP1</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.7.4</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>4.2.7.SP1</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>

        <dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.7</version>
    <scope>provided</scope>
</dependency>
<!-- if you are using Jersey client specific features -->
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.7</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-processing</artifactId>
    <version>2.7</version>
</dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

没有例外。显示未找到的错误是什么?

2 个答案:

答案 0 :(得分:1)

我弄清了它的问题因为:我的web.xml中有两个init参数

a) Application
b) package path

我相信application mapping会覆盖package param mapping,然后在我的应用程序类中,我必须显式注册我的资源类。

我希望这很清楚。

答案 1 :(得分:0)

如果您使用的是Glassfish 4,请尝试删除web.xml中的LoggingFilter参数和<servlet-class>行。它应该工作。我仍然不明白为什么,但我遇到了同样的问题,试图设置LoggingFilter。

编辑:我设法使用本教程使其工作: http://blog.dejavu.sk/2013/11/19/registering-resources-and-providers-in-jersey-2/

它还表示您不需要CalculatorJerseyApplication课程。 此外,如果您的资源位于不同的包中,则必须在

中设置根包

<param-name>jersey.config.server.provider.packages</param-name>

然后将递归扫描参数设置为true:

<init-param> <param-name>jersey.config.server.provider.scanning.recursive</param-name> <param-value>false</param-value> </init-param>