Restful Web Service返回404,错误的web.xml? [英] Restful Web Service returning 404, bad web.xml?

查看:86
本文介绍了Restful Web Service返回404,错误的web.xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还有其他人问这个问题,但是他们得到的答案对我没有用.我正在运行泽西休息服务器,如Tomcat中的链接所述7.我的Resource类是Hello,如下所示:

There are other people asking this question, but the answers they got aren't working for me. I'm running a Jersey rest server as discussed in this link in Tomcat 7. My Resource class is Hello as shown below:

package com.rest.videos;

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 request
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello Jersey";
  }

  // This method is called if XML is request
  @GET
  @Produces(MediaType.TEXT_XML)
  public String sayXMLHello() {
    return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
  }

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

}

我的web.xml文件如下:

My web.xml file is the following:

<?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"
    version="3.0">
    <display-name>Videos</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <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.rest.videos</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletAdaptor</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

每当我访问URL http://localhost:8080/Videos/rest/hello时,它都会返回404.我的WEB-INF目录中有一个index.html文件,如果我转到http://localhost:8080/Videos/,我认为应该是因为welcome-file-list这样说,我也得到了404.

Whenever I visit the URL http://localhost:8080/Videos/rest/hello, it returns 404. I have an index.html file in my WEB-INF directory and if I go to http://localhost:8080/Videos/ where I assume it should be since the welcome-file-list says so, I also get a 404.

我在做什么错了?

推荐答案

好,如果您不介意提供积分,我会发布答案:)

Ok, I'll post an answer then if you don't mind giving credits :)

web.xml与URI路径无关,因此,除非将您的WAR包装到带有/Videos的EAR中,否则此前缀无效,并且很可能是您404的根.

web.xml has not bearing on the URI path, so unless your WAR is wrapped into an EAR w/ a of /Videos, this prefix is invalid and likely is the root of your 404s.

这篇关于Restful Web Service返回404,错误的web.xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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