简体   繁体   English

泽西tomcat和日食:错误404

[英]Jersey tomcat and eclipse: error 404

I was following vogella "REST with Java (JAX-RS) using Jersey - Tutorial". 我正在关注vogella“使用Jersey的Java REST(JAX-RS)-教程”。

I created a new dynamic web project named "project" generating web.xml file. 我创建了一个新的动态Web项目,名为“ project”,生成了web.xml文件。

I added all jar files from jaxrs-ri-2.23.1 in "eclipseWorkspace/project/WebContent/WEB-INF/lib" 我在“ eclipseWorkspace / project / WebContent / WEB-INF / lib”中添加了来自jaxrs-ri-2.23.1的所有jar文件

I created a new class named Hello and copied the content from vogella tutorial: 我创建了一个名为Hello的新类,并复制了vogella教程中的内容:

package project;

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

// Plain old Java Object it does not extend as class or implements 
// an interface

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello
@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> ";
  }

} 

I modified web.xml in this way: 我以这种方式修改了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" 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>project</display-name>
 <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
     <!-- Register resources and providers under com.vogella.jersey.first package. -->
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>project</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>
</web-app>

Right click on project name, run as, Run on server. 右键单击项目名称,运行方式为,在服务器上运行。 Choosed the existing tomcat v7 server at localhost. 选择本地主机上现有的tomcat v7服务器。

Restarted server. 重新启动服务器。

When i go to: 当我去:

http://localhost:8080/project/rest/hello http:// localhost:8080 / project / rest / hello

It shows 404 error: 它显示404错误:

HTTP Status 404 - Not Found HTTP状态404-找不到

type Status report 类型状态报告

message Not Found 未找到信息

description The requested resource is not available. 描述所请求的资源不可用。

Apache Tomcat/7.0.70 Apache Tomcat / 7.0.70

Do you know how to solve this? 你知道如何解决吗?

Solved unselecting the option "Build Automatically" in eclipse. 解决了在eclipse中取消选择“自动构建”选项的问题。

Then "Build project" and "Run on server" and it works. 然后“构建项目”和“在服务器上运行”即可。

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

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