尝试使用Jersey创建一个简单的RESTful Web服务并将其部署在Tomcat 7上。由于我是Java新手,并渴望学习它,所以我跟随this tutorial
我在Eclipse中将此应用程序创建为Maven项目,并将jersey-quickstart-webapp
工件版本2.22.2
与org.glassfish.jersey.archetypes
groupID一起使用。请注意,我必须稍后将Project Facet更改为Dynamic Web Module
否则它不会给我一个将其部署到本地Tomcat服务器的选项。
我已将messenger
应用添加为Tomcat服务器中的资源。在Eclipse中启动Tomcat服务器时,我没有看到任何错误。只有一个警告
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:messenger' did not find a matching property.
,根据很多SO帖子,可以安全地忽略。
右键点击该应用并点击Run As
- > Run on Server
,我发现网址http://localhost:8080/messenger/
以及http://localhost:8080/webapi/myresource
找不到404页面。
检查过很多类似的SO帖子,例如this和this但是他们没有帮助。
我注意到的一件事是web.xml
内的<peoject_root>/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ROOT/WEB-INF
与我的应用的web.xml不匹配(在部署应用后不确定它是否匹配)到Tomcat)。事实上,该目录下的内容复制如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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" version="2.5">
需要一些关于可能出错的指示。这可能是一件简单的事情,但是我花了很多时间在这个问题上挣扎并在第n次出现同样的404错误。在我有信心并继续观看培训视频之前,我希望能够让这个简单的应用程序工作。
通过屏幕截图和文件内容添加更多细节:
MyResource.java
package org.testapp.java.messenger;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
* Root resource (exposed at "myresource" path)
*/
@Path("myresource")
public class MyResource {
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return "Got it!";
}
}
的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>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.testapp.java.messenger</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
项目结构
Tomcat错误
您在代码中看到的一个RED十字架是实际The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
,教程视频中的讲师要求忽略,因为Tomcat提供了HTTPServlet类。
控制台输出
May 01, 2016 10:09:20 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:messenger' did not find a matching property.
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/7.0.69
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Apr 11 2016 07:57:09 UTC
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 7.0.69.0
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Mac OS X
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.10.2
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: x86_64
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: /Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home/jre
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_31-b13
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: /Users/abc/Personal/Java/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: /Users/abc/Personal/Java/apache-tomcat-7.0.69
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/Users/abc/Personal/Java/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/Users/abc/Personal/Java/apache-tomcat-7.0.69
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/Users/abc/Personal/Java/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/Users/abc/Personal/Java/apache-tomcat-7.0.69/endorsed
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
May 01, 2016 10:09:20 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/abc/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
May 01, 2016 10:09:20 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
May 01, 2016 10:09:20 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
May 01, 2016 10:09:20 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 434 ms
May 01, 2016 10:09:20 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
May 01, 2016 10:09:20 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.69
May 01, 2016 10:09:20 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
May 01, 2016 10:09:20 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
May 01, 2016 10:09:20 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 269 ms
的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>org.testapp.java</groupId>
<artifactId>messenger</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>messenger</name>
<build>
<finalName>messenger</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.7</source>
<target>1.7</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-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
-->
</dependencies>
<properties>
<jersey.version>2.22.2</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
答案 0 :(得分:1)
在servlet条目之前,在web.xml中添加以下行
<display-name>messenger</display-name>
答案 1 :(得分:1)
在学习该教程时,我也遇到了同样的问题,尝试了几件事,但是它不起作用。 最后,当我将版本降级到2.6(部分测试和错误)时,它显示“ Git it”作为响应。我最初为Jersey原型选择了最新版本3.0.0-M6。
答案 2 :(得分:1)
我遇到了类似的问题,意识到Jersey 3.0.0-M6和Tomcat 9不兼容。
原因-> Tomcat 9使用javax.servlet.Filter。雅加达EE 9的一部分Jersey 3.x依赖于基于雅加达的API软件包。 jakarta.servlet.Filter在Tomcat 10中可用。
解决方案->下载Tomcat 10并使用它。或在Tomcat 9或更低版本中使用Jersey 2.X版。
我个人将Jersey 2.16与Tomcat 9一起使用,并且对我有用。
答案 3 :(得分:-1)
<强>更新强>
我尝试在Jetty上部署我的应用程序,方法是将以下插件添加到pom.xml中的build部分。
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
</plugin>
然后从终端运行mvn jetty:run
。现在,我可以在http://localhost:8080/messenger/webapi/myresource
成功访问我的网络应用。它按预期返回Got it!
。
所以,这肯定是我将它部署到tomcat的方式的问题。我有兴趣进一步调试tomcat问题,如果有人有一些指针。