Java 动态网络服务器 - 无法读取 'hello world' 球衣资源
Java dynamic webserver - unable to read 'hello world' jersey resource
我使用的是 jersey 2.0,我已经使用这个 site
将 jersey jar 文件安装到 eclipse 中
我遇到的问题是无法加载 hello world 项目。
这是我的项目结构:
这是位于 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>
这是你好世界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> ";
}
}
我遇到的问题是 运行 服务器说找不到资源。这是 link:
http://localhost:8080/com.vogella.web.filecounter2/rest/hello
更新:这是我拥有的所有 jar 文件的列表:
这是我尝试在 Eclipse 或 chrome 上查看资源时遇到的错误:
您 web.xml
中的代码需要更正。
<init-param>
<param-name>com.vogella.web.resources</param-name>
<param-value>com.vogella.web.filecounter2</param-value>
</init-param>
param-name
和 param-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>
我使用的是 jersey 2.0,我已经使用这个 site
将 jersey jar 文件安装到 eclipse 中我遇到的问题是无法加载 hello world 项目。
这是我的项目结构:
这是位于 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>
这是你好世界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> ";
}
}
我遇到的问题是 运行 服务器说找不到资源。这是 link:
http://localhost:8080/com.vogella.web.filecounter2/rest/hello
更新:这是我拥有的所有 jar 文件的列表:
这是我尝试在 Eclipse 或 chrome 上查看资源时遇到的错误:
您 web.xml
中的代码需要更正。
<init-param>
<param-name>com.vogella.web.resources</param-name>
<param-value>com.vogella.web.filecounter2</param-value>
</init-param>
param-name
和 param-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>