Maven WebApp 中的 Jetty SERVICE_UNAVAILABLE 与 Eclipse 中的 Jersey JAX-RS

发布于 2024-11-15 07:42:43 字数 6904 浏览 1 评论 0原文

尝试在 Tomcat 中运行我的 WebApp 示例但未成功后,我按照本教程进行操作,以获得一个简单的 HelloWorld JAX-RS 应用程序,使用 Maven 在 Eclipse 中运行。不幸的是,当我执行 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>

MyServlet.java 在 src/main/resources

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;
    }

After Trying to run my WebApp Example in Tomcat, not succeeding I followed the this tutorial in order to get a simple HelloWorld JAX-RS application running in Eclipse with Maven. Unfortunately when I execute mvn jetty:run and open http://localhost:8088/jersey-demo/hello/ in a browser I only get back:

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>

MyServlet.java in src/main/resources

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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埋情葬爱 2024-11-22 07:42:43

从工件球衣服务器中删除条目 pom,它应该可以工作。

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

Remove the entry <type>pom</type> from artifact jersey-server and it should work.

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.7</version>
    <scope>compile</scope>
</dependency>
絕版丫頭 2024-11-22 07:42:43

在 Eclipse 中,转到 Windows -> 首选项 -> Maven -> 用户设置并确保您的.m2目录下有settings.xml文件。

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

In Eclipse, goto Windows -> Preferences -> Maven -> User Settings and make sure you have point settings.xml file under your .m2 directory there.

Also check the second second field Local Repository has correct repository address.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文