Java动态网络服务器 - 无法读取'hello world'球衣资源

时间:2015-07-07 15:12:42

标签: java eclipse rest jersey-2.0

我正在使用jersey 2.0,我使用此site

将jersey jar文件安装到eclipse中

我遇到的问题是我无法加载一个hello world项目。

这是我的项目结构:

enter image description here

这是位于WEB-INF中的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>com.vogella.web.filecounter2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <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>com.vogella.web.resources</param-name>
        <param-value>com.vogella.web.filecounter2</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>

这是hello world class:

 package com.vogella.web.resources;

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

//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> ";
    }

}

我遇到的问题是当我运行服务器时它说资源未找到。这是链接:

http://localhost:8080/com.vogella.web.filecounter2/rest/hello

更新:这是我拥有的所有jar文件的列表:

enter image description here

这是我在尝试在eclipse或chrome上查看资源时遇到的错误:

enter image description here

1 个答案:

答案 0 :(得分:0)

web.xml中的代码需要更正。

<init-param> 
    <param-name>com.vogella.web.resources</param-name> 
    <param-value>com.vogella.web.filecounter2</param-value> 
</init-param>

param-nameparam-value字段不正确。将param-name更改为jersey.config.server.provider.packages,将param-value更改为com.vogella.web.resources

修正应该看起来像

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>com.vogella.web.resources</param-value>
</init-param>