Maven WebApp 中的 Jetty SERVICE_UNAVAILABLE 与 Eclipse 中的 Jersey JAX-RS
尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从工件球衣服务器中删除条目
pom
,它应该可以工作。Remove the entry
from artifact jersey-server and it should work.<type>pom</type>
在 Eclipse 中,转到
Windows
->首选项
->Maven
->用户设置
并确保您的.m2
目录下有settings.xml
文件。另请检查第二个字段
Local Repository
是否具有正确的存储库地址。In Eclipse, goto
Windows
->Preferences
->Maven
->User Settings
and make sure you have pointsettings.xml
file under your.m2
directory there.Also check the second second field
Local Repository
has correct repository address.