404访问Tomcat 7中的Jersey应用程序时 [英] 404 when accessing Jersey app in Tomcat 7

查看:137
本文介绍了404访问Tomcat 7中的Jersey应用程序时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是网络开发的新手,我正在努力创建一个简单的休息网络服务,使用jersey打包作为一个独立的war文件,将部署在tomcat 7上。

I am a newby in web development and I am struggling to create a simple rest web service using jersey, packed as an independent war file, to be deployed on tomcat 7.

我已按照本教程进行创建简单的你好世界宁静的网络服务。
我使用intelij来创建war文件(war文件和它的爆炸版本)并将它们部署在tomcat\webapps文件夹中。

I have followed this tutorial in order to create simple hello world restful web service. I have used intelij in order to create the war file (both war file and exploded version of it) and deployed them on the tomcat\webapps folder.

启动tomcat时,我无法访问URL( http:// localhost:8080 / rest / hello ) - 获得404。

When starting up the tomcat, I fail to access the url (http://localhost:8080/rest/hello) - getting 404.

查看tomcat的日志,我找不到任何异常或错误,除了下一组行之外:

Looking at the tomcat's logs, I can't find any exception or error, other then these next set of lines:

INFO: Scanning for root resource and provider classes in the packages:
  sample
εαΘ 30, 2013 11:24:38 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class sample.Hello
εαΘ 30, 2013 11:24:38 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.

我搜索了很多内容,并尝试按照所有解决方案解决此错误,但没有任何效果。

I have searched it a lot and tried to follow all solutions for this error but nothing worked.

由于我的新生,我觉得我错过了一些非常基本的东西。
任何想法搜索什么?我错过了什么?
这是一个简单的你好世界的应用程序,我很兴奋和沮丧...

I have the feeling that I missing something very basic, due to my newbi'ness. Any ideas what to search ? what am I missing ? it's a simple hello world app and I am exahuster and frustrated...

Java类

package sample;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("hello")
public class Hello {

  // This method is called if TEXT_PLAIN is requested
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayHelloInPlainText() {
    return "Hello world!";
  }

  // This method is called if HTML is requested
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHelloInHtml() {
    return "<html> " + "<title>" + "Hello world!" + "</title>"
        + "<body><h1>" + "Hello world!" + "</body></h1>" + "</html> ";
  }
}

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

      <display-name>rest</display-name>

      <servlet>
        <servlet-name>Jersey REST Service</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>sample</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>

      <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/*</url-pattern>
      </servlet-mapping>

    </web-app>


推荐答案

这是类似到这个问题:找不到提供程序类:运行Jersey REST示例应用程序时

消息找不到提供者类很好,无需担心。对于要运行WAR文件的URL http:// localhost:8080 / rest / hello ,需要调用 rest.war 。然后 rest 成为上下文根。您在项目注释中定义的所有路径都是相对于上下文根

The message "No provider classes found" is fine, no need to worry about it. For the URL http://localhost:8080/rest/hello to work your WAR file needs to be called rest.war. Then rest becomes the context root. All the paths you define in the project's annotations are relative to the context root.

这篇关于404访问Tomcat 7中的Jersey应用程序时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆