简体   繁体   English

使用Java,Eclipse的RESTful Web服务创建教程。 Apache Tomcat,无法正常工作

[英]RESTful web service creation tutorial with Java, Eclipse. Apache Tomcat, not working

I'd like to create and run a simple RESTful application on my pc. 我想在我的电脑上创建并运行一个简单的RESTful应用程序。 I'm using Java programming language, on Eclipse Helios IDE, with Apache Tomcat 7 server, on a Microsoft Windows Xp operating system. 我在Microsoft Windows Xp操作系统上使用Eclipse Helios IDE上的Java编程语言和Apache Tomcat 7服务器。

I've been trying to replicate the same procedure of this tutorial (from 3 to 3.4 steps) on my pc, but it does not work. 我一直试图在我的电脑上复制本教程的相同程序(从3到3.4步) ,但它不起作用。

Here's what I did: 这是我做的:

  • created a Dynamic project on Eclipse, with Apache Tomcat 7 settings 使用Apache Tomcat 7设置在Eclipse上创建了一个Dynamic项目

  • put my Jersey and jsr311-api-1.0 jar files into the project \\WEB-INF\\lib\\ directory 把我的Jersey和jsr311-api-1.0 jar文件放到project \\WEB-INF\\lib\\目录下

  • copied the code parts from the webpage to my project 将代码部分从网页复制到我的项目中

  • right-clicked on the project, clicked on "Run as -> Run on server" 右键单击该项目,单击“运行方式 - >在服务器上运行”

  • tried to reach the http://localhost:8080/de.vogella.jersey.first/rest/hello 试图到达http://localhost:8080/de.vogella.jersey.first/rest/hello

But there's what I see: 但是我看到的是:

休息截图

Why does it not work? 为什么不起作用? What did I do wrong? 我做错了什么? Why? 为什么?

Many thanks! 非常感谢!

Edit: 编辑:

Here's what the console says: 以下是控制台所说的内容:

25-set-2012 9.35.07 org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Programmi\Java\jdk1.6.0_21\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Programmi/Java/jre6/bin/client;C:/Programmi/Java/jre6/bin;C:/Programmi/Java/jre6/lib/i386;C:\WINDOWS\;C:\WINDOWS\System32;"C:\ApacheMaven\apache-maven-3.0.4\bin";C:\WINDOWS\;C:\WINDOWS\System32;"C:\ApacheMaven\apache-maven-3.0.4\bin";C:\WINDOWS\;C:\WINDOWS\System32;"C:\ApacheMaven\apache-maven-3.0.4\bin";C:\WINDOWS\System32;C:\WINDOWS\;C:\WINDOWS\System32; C:\Programmi\R-Language\R-2.13.1\bin\;C:\Programmi\OpenVPN\bin\; C:\Programmi\Java\jdk1.6.0_21\bin\; "C:\ApacheMaven\apache-maven-3.0.4\bin";;C:\Programmi\eclipseEC;
25-set-2012 9.35.08 org.apache.tomcat.util.digester.SetPropertiesRule begin
AVVERTENZA: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:de.vogella.jersey.first' did not find a matching property.
25-set-2012 9.35.10 org.apache.coyote.AbstractProtocolHandler init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
25-set-2012 9.35.10 org.apache.coyote.AbstractProtocolHandler init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
25-set-2012 9.35.10 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 3459 ms
25-set-2012 9.35.10 org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
25-set-2012 9.35.10 org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.6
25-set-2012 9.35.11 org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:com:sun:jersey:api:view is already defined
25-set-2012 9.35.12 org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:com:sun:jersey:api:view is already defined
25-set-2012 9.35.12 org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:com:sun:jersey:api:view is already defined
25-set-2012 9.35.12 org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet Jersey REST Service as unavailable
25-set-2012 9.35.12 org.apache.catalina.core.StandardContext loadOnStartup
GRAVE: Servlet /de.vogella.jersey.first threw load() exception
java.lang.ClassNotFoundException: javax.servlet.Filter
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1488)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:415)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:397)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1048)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:996)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4741)
    at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5062)
    at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5057)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)
25-set-2012 9.35.12 org.apache.coyote.AbstractProtocolHandler start
INFO: Starting ProtocolHandler ["http-bio-8080"]
25-set-2012 9.35.12 org.apache.coyote.AbstractProtocolHandler start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
25-set-2012 9.35.12 org.apache.catalina.startup.Catalina start
INFO: Server startup in 1790 ms

Maybe there's some class missing? 也许有些课程失踪了?

To solve your problem, without using Maven, you need to add JAX-RS facet to your project. 要解决您的问题,不使用Maven,您需要将JAX-RS facet添加到项目中。 What's happening here is that Eclipse is not deploying the right libs to the server . 这里发生的是Eclipse没有将正确的lib部署到服务器 This configuration can be accessed through your project's Properties | 可以通过项目的Properties |访问此配置 Project's Facets. 项目的方面。

To be honest I don't know which specific jars to add to make Jersey work, because I use Maven to manage dependencies. 说实话,我不知道要添加哪些特定的罐子来使Jersey工作,因为我使用Maven来管理依赖。 So instead of the zip file, I've downloaded the Jersey bundle , and configured the JAX-RS facet with it. 因此,我已经下载了Jersey软件包 ,并使用它配置了JAX-RS方面,而不是zip文件。 The effect of this can be seen on the project's Properties | 这可以在项目的属性|中看到 Deployment Assembly, where Jersey's libs are already configured to be deployed. 部署程序集,其中Jersey的libs已配置为已部署。 By the way is there you can manually setup the deployed libs to the server. 顺便提一下,您可以手动将已部署的库设置为服务器。

But then I remembered why I use Maven to manage dependencies. 但后来我想起了为什么我使用Maven来管理依赖项。 The bundle does not contain everything it needs to run Jersey. 该捆绑包不包含运行Jersey所需的所有内容。 It's missing the asm jar (present in the zip file). 它缺少asm jar(存在于zip文件中)。 So I had to add it to projects build path, and reconfigure de deployment assembly manually.... 所以我不得不将它添加到项目构建路径,并手动重新配置部署组件....

So, here is a tutorial with Maven, Jersey and Eclipse. 所以,这是Maven,Jersey和Eclipse的教程 Try it and draw your conclusions :) 试一试,得出你的结论:)

I went through the same tutorial to develop my first RESTFUL service. 我通过相同的教程开发了我的第一个RESTFUL服务。 In the beginning i also got 404 errors few times so i kept changing few things, finally it worked for me. 一开始我也有几次404错误所以我不断改变一些事情,最后它对我有用。 This is what i did- 这就是我所做的 -

1) removed the rest from the URL-Pattern tab; 1)从URL-Pattern选项卡中删除其余部分; i think that is there by mistake. 我认为这是错误的。 2) used the context root in the URL rather than the package name. 2)使用URL中的上下文根而不是包名。

my url is, "http://mylocalhost:8080/contextroot/classname that is defined in the @path annotation. 我的网址是"http://mylocalhost:8080/contextroot/classname ,它是在@path注释中定义的。

hope it helps. 希望能帮助到你。

The servlet.jar shouldn't be at the resulting war file (or in WEB-INF/lib). servlet.jar不应该出现在生成的war文件中(或者在WEB-INF / lib中)。 It should be provided by the application server itself. 它应该由应用程序服务器本身提供。 (see Tomcat's FAQ ) If you are using the Maven for building the project, you may set the scope of the servlet api as provided ( link ). (请参阅Tomcat的常见问题解答 )如果您使用Maven构建项目,则可以按provided servlet api的范围( 链接 )。

Another similar question here on StackOverflow . StackOverflow上的另一个类似问题。

I repeated your example and it works out of the box on my machine. 我重复了你的例子,它在我的机器上开箱即用。 I downloaded jersey-archive-1.14.zip and put the whole content of the included lib directory into WEB-INF/lib, not just Jersey and jsr311-api-1.0 jar as you did. 我下载了jersey-archive-1.14.zip并将包含的lib目录的全部内容放入WEB-INF / lib,而不仅仅是Jerseyjsr311-api-1.0 jar

The jars are: 这些罐子是:

  • asm-3.1.jar ASM-3.1.jar
  • jackson-core-asl-1.9.2.jar 杰克逊核心ASL-1.9.2.jar
  • jackson-jaxrs-1.9.2.jar 杰克逊JAXRS-1.9.2.jar
  • jackson-mapper-asl-1.9.2.jar 杰克逊映射器-ASL-1.9.2.jar
  • jackson-xc-1.9.2.jar 杰克逊-XC-1.9.2.jar
  • jersey-client-1.14.jar 新泽西州的客户 - 1.14.jar
  • jersey-core-1.14.jar 新泽西州的核心1.14.jar
  • jersey-json-1.14.jar 新泽西州JSON-1.14.jar
  • jersey-server-1.14.jar 新泽西服务器1.14.jar
  • jersey-servlet-1.14.jar 新泽西州的servlet-1.14.jar
  • jettison-1.1.jar 抛放-1.1.jar
  • jsr311-api-1.1.1.jar JSR311的API-1.1.1.jar

Some of them might by unnecessary, you could try to reduce them. 其中一些可能是不必要的,你可以尝试减少它们。 But start with all of them. 但从所有这些开始。

The error message is strange, though. 但错误信息很奇怪。 It says that javax.servlet.Filter is missing, which should live in <your tomcat dir>/lib/servlet-api.jar . 它说缺少javax.servlet.Filter ,它应该存在于<your tomcat dir>/lib/servlet-api.jar This file is always on the classpath when you run tomcat. 运行tomcat时,此文件始终位于类路径中。

You need to remove servlet.jar or any other jar containing javax.servlet.Filter from your WEB-INF/lib directory. 您需要从WEB-INF / lib目录中删除servlet.jar或包含javax.servlet.Filter的任何其他jar。

While you will need the class javax.servlet.Filter on your compile classpath in order for the sample code to compile, you need to ensure that you do not include this class in your webapp. 虽然您需要在编译类路径上使用类javax.servlet.Filter以便编译示例代码,但您需要确保不在Web应用程序中包含此类。 From the Tomcat class loader HOWTO: 从Tomcat类加载器HOWTO:

When Tomcat is started, it creates a set of class loaders that are organized into the following parent-child relationships, where the parent class loader is above the child class loader: 当Tomcat启动时,它会创建一组类加载器,这些加载器被组织成以下父子关系,其中父类加载器位于子类加载器之上:

  Bootstrap | System | Common / \\ 

Webapp1 Webapp2 ... Webapp1 Webapp2 ...

Basically, the (probably) servlet.jar in the webapp is probably not being found properly d/t the security configured in the Tomcat container. 基本上,在Tomcat容器中配置的安全性可能无法正确找到webapp中的(可能)servlet.jar。

You can see if javax.servlet.Filter is being loaded from a weird location by turning on verbose class loading verbose:class . 您可以通过打开详细类加载verbose:class来查看是否从奇怪的位置加载了javax.servlet.Filter。 Search for the class, if it's not coming from the Tomcat libs, that is likely your problem. 搜索类,如果它不是来自Tomcat库,那可能是你的问题。

May be you are missing servlet-api.jar. 可能是你缺少servlet-api.jar。 Add latest jar in your /WEB-INF/lib or classpath and see of it works or not. 在/ WEB-INF / lib或类路径中添加最新的jar,看看它是否有效。

It seems that it does not find the servlet jersey jars in the classpath. 它似乎没有在类路径中找到servlet jersey jar。 Make sure to include them. 确保包含它们。 I assume that you package into a war and deploy before you test this right. 我假设你在测试这个权利之前打包成战并部署。 So just check if the war includes all the jars that were available to your application at compile time. 因此,只需检查战争是否包含编译时应用程序可用的所有jar。

Like previous answers mentioned. 像之前的答案一样。 You are missing some jars. 你错过了一些罐子。 You can either add those jar files in Project>WebContent>WEB-INF>lib> folder or in your webserver's lib folder (for example, \\apache-tomcat\\lib ) 您可以在Project> WebContent> WEB-INF> lib>文件夹中或在webserver的lib文件夹中添加这些jar文件(例如, \\ apache-tomcat \\ lib

Try pasting list or screenshot of your both lib folders, if you'd like us to tell you what exact jar you are missing. 如果您希望我们告诉您缺少什么样的jar,请尝试粘贴两个lib文件夹的列表或屏幕截图。

Hope this helps 希望这可以帮助

As far as I can tell, you are running Tomcat from within Eclipse. 据我所知,您正在Eclipse中运行Tomcat。 Is is possible you are not using the installation directory in your computer (ie the one where you have copied the JARs to) but are in fact using the temporary copy that Eclipse creates locally (sorry, I'm not on my dev computer atm so can't check the details)? 有可能你没有在计算机中使用安装目录(即你已经将JAR复制到的那个),但实际上是使用Eclipse在本地创建的临时副本(对不起,我不在我的开发计算机上,所以无法查看详细信息)?

From memory you have 2 options: 从记忆中你有2个选择:

  1. You need to change the (working) directory that Eclipse is using for the Tomcat to be the one where you actually have the Tomcat files. 您需要更改Eclipse用于Tomcat的(工作)目录,使其成为您实际拥有Tomcat文件的目录。

  2. You need to add the JARs to the Tomcat in Eclipse with the GUI interface within Eclipse. 您需要使用Eclipse中的GUI界面将JAR添加到Eclipse中的Tomcat。

Alternatively, I could be completely wrong here ;-) 或者,我在这里可能完全错了;-)

EDIT: Including a screenshot of the bit I'm talking about. 编辑:包括我正在谈论的位的截图。

For my options above: 对于我上面的选择:

  1. See the "Sever Locations" in the middle needs to be in the one that "Takes control of the tomcat installation". 请参阅中间的“Sever Locations”,使其成为“控制tomcat安装”。
  2. You add them in the classpath tab when you click the "Open launch configuration" link in the "General Information" box in the image below 单击下图中“常规信息”框中的“打开启动配置”链接时,可以在类路径选项卡中添加它们

在此输入图像描述

您应该添加所有在jersey 1.6存档中找到的jar,下载存档并将所有jar添加到项目的WebContent / WEB-INF / lib文件夹中。

I had this error for more than one day! 我有这个错误超过一天! It turns out that the problem comes from the usage of different versions of Jersey. 事实证明,问题来自使用不同版本的泽西岛。 If you replace your web.xml content by the content I present below, it will be fine! 如果您使用我在下面提供的内容替换您的web.xml内容,那就没问题!

    <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>    
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>de.vogella.jersey.first</param-value>
    </init-param>
    <init-param>
     <param-name>unit:WidgetPU</param-name>
     <param-value>persistence/widget</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>

Hopefully, my answer will save lots of time for someone else who also decides to follow the same tutorial! 希望我的答案能为那些决定遵循相同教程的其他人节省大量时间!

Delete the servlet.jar from WEB-INF/lib folder. 从WEB-INF / lib文件夹中删除servlet.jar。 Servlet.jar shouldn't be there this fixed the problem for me. Servlet.jar不应该在那里解决这个问题。

I had the HTTP Status 404 issue too, following the same tutorial. 我也遇到了HTTP状态404问题,遵循相同的教程。

First I tried to set JAX-RS Project Facet in Eclipse with either Jersey libraries 1.18 and 2.15 but it didn't work. 首先,我尝试使用Jersey库1.18和2.15在Eclipse中设置JAX-RS Project Facet,但它不起作用。

Finally I solved the problem by replacing the package name (de.vogella.jersey.first) in the URL with the context root (ie the eclipse project name). 最后,我通过用上下文根(即eclipse项目名称)替换URL中的包名(de.vogella.jersey.first)来解决问题。

This tutorial by mkyong.com gave me the hint to find the solution. mkyong.com的这个教程给了我提示找到解决方案。 Anyway I don't know if the package name in the URL is definitely a mistake or if there are some configuration details that could make it work as well. 无论如何,我不知道URL中的包名是否肯定是一个错误,或者是否有一些配置细节可以使它工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM