在Eclipse中使用Jersey JAX-RS的Maven WebApp中的Jetty SERVICE_UNAVAILABLE

时间:2011-06-14 06:56:11

标签: java eclipse maven jetty jersey

尝试在Tomcat中运行我的WebApp示例后,没有成功,我遵循this tutorial以获得在Eclipse中使用Maven运行的简单HelloWorld JAX-RS应用程序。不幸的是,当我在浏览器中执行mvn jetty:run并打开http://localhost:8088/jersey-demo/hello/时,我才会回来:

HTTP ERROR: 503

SERVICE_UNAVAILABLE
RequestURI=/jersey-demo/hello

的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.ele.api</groupId>
  <artifactId>server</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>server Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.1</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.13</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.6.0</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.16</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.7</version>
        <type>pom</type>
        <scope>compile</scope>
    </dependency>
  </dependencies>
  <repositories>
      <repository>
        <id>maven2-repository.java.net</id>
        <name>Java.net Repository for Maven</name>
        <url>http://download.java.net/maven/2/</url>
        <layout>default</layout>
    </repository> 
    <repository>
        <id>maven-repository.java.net</id>
        <name>Java.net Maven 1 Repository (legacy)</name>
        <url>http://download.java.net/maven/1</url>
        <layout>legacy</layout>
    </repository>
  </repositories>
    <build>
      <plugins>
        <plugin>
        <!-- JETTY 6 PLUGIN -->
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.14</version>
        <configuration>
            <scanIntervalSeconds>10</scanIntervalSeconds>
            <contextPath>/jersey-demo</contextPath>
            <connectors>
                <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                    <port>8088</port>
                    <maxIdleTime>60000</maxIdleTime>
                </connector>
            </connectors>
            <webApp>${basedir}/target/jersey-demo</webApp>
            <requestLog implementation="org.mortbay.jetty.NCSARequestLog">
                <filename>target/yyyy_mm_dd.request.log</filename>
                <retainDays>90</retainDays>
                <append>true</append>
                <extended>true</extended>
                <logTimeZone>GMT</logTimeZone>
            </requestLog>
        </configuration>
           </plugin>
       </plugins>
    </build>
</project>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <display-name>Jersey Demo</display-name>
    <servlet>
        <servlet-name>ServletAdaptor</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.ele.api</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletAdaptor</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

src / main / resources中的MyServlet.java

package com.ele.api

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.apache.log4j.Logger;

/**
 * Jersey Demo
 */
@Path("/hello")
public class MyServlet {

    private static final Logger LOGGER = Logger.getLogger(MyServlet.class);

    /**
     * 
     * @return
     */
    @GET
    @Produces("text/plain")
    public String doGet() {
        return "hello";
    }

    /**
     * 
     * @param xml
     * @return
     */
    @POST
    @Produces("application/xml")
    @Consumes({"application/x-www-form-urlencoded", "multipart/form-data"})
    public String doPost(@FormParam("xml") String xml) {
        if(xml == null) { 
            LOGGER.error("Expected 'xml' parameter was not found in the POST");
            return null;
        }
        return xml;
    }

2 个答案:

答案 0 :(得分:1)

从artifact jersey-server中删除条目 <type>pom</type> ,它应该可以正常工作。

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.7</version>
    <scope>compile</scope>
</dependency>

答案 1 :(得分:0)

在Eclipse中,转到Windows - &gt; Preferences - &gt; Maven - &gt; User Settings并确保您的settings.xml目录下有.m2个点。

同时检查第二个字段Local Repository是否具有正确的存储库地址。