【发布时间】:2018-05-15 01:37:35
【问题描述】:
我已经根据此处步骤 6.3 之前描述的详细信息配置了球衣(带有 tomcat 服务器): http://www.vogella.com/tutorials/REST/article.html#jerseyprojectsetup
下面是我的web.xml文件,唯一的区别是jersey.config.server.provider.packages的路径/包名
说明书有;
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.vogella.jersey.first</param-value>
因为com.vogella.jersey.first 是他们的包名,而我选择将我的指向我的默认包(jerseytest)
但这是我的 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>jerseytest</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>jerseytest</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>
当我在 tomcat 服务器上运行应用程序时,我不断收到:
HTTP 状态 404 - 未找到
类型状态报告
Message /jerseytest/描述 源服务器没有找到当前的表示 对于目标资源或不愿意透露存在的资源。
Apache Tomcat/8.5.23
当我将浏览器指向教程中提到的终点时:
http://localhost:8080/com.vogella.jersey.first/rest/hello
我收到一个未找到的错误:
HTTP 状态 404 - 未找到
类型状态报告
Message Not Found描述 源服务器没有找到当前的表示 对于目标资源或不愿意透露存在的资源。
Apache Tomcat/8.5.23
当我将浏览器直接指向http://localhost:8080/rest/hello时
我明白了:
HTTP 状态 404 - 未找到
类型状态报告
Message /rest/hello描述 源服务器没有找到当前的表示 对于目标资源或不愿意透露存在的资源。
Apache Tomcat/8.5.23
我的 java 类与说明中的示例相同,除了我的是在默认包中:
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> ";
}
}
【问题讨论】:
-
尝试通过转到 project->property-facets 来添加和更改项目的方面