【发布时间】:2015-07-11 11:27:56
【问题描述】:
我正在尝试让我的第一个 Jersey Web 服务项目工作,但我收到此错误 Jersey: The requested resource is not available. 我已通过 eclispe Maven 安装了 Jersey 2.16 并安装了 Tomcat 8.0.21 我已在 @987654325 中创建了 MessageResource.java 类@
我正在输入此链接:
http://localhost:8080/messanger/webapi/messages
如果我点击此链接http://localhost:8080/messanger/webapi/myresource 我会收到Got it!
我用这些数据添加了带有 maven 的 Jersy:
- org.glassfish.jersey.archetypes
- jersey-Quickstart-webapp
- 2.16
MessageResource.java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/messages")
public class MessageResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getMessages(){
return "Hello world";
}
}
工作代码:
package org.test.messanger;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
* Root resource (exposed at "myresource" path)
*/
@Path("myresource")
public class MyResource {
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return "Got it!";
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.test.messanger</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
【问题讨论】:
-
请停止删除您的帖子,并再次发布确切相同的问题。这已经是第三次了。
-
您向我们展示的内容看起来不错。问题在于您没有向我们展示或告诉我们的东西。只是为了让您知道您可能没有得到答案的原因是因为问题无法从您提供的内容中重现或发现。这并不是说你没有提供一个很好的例子/很好的问题,这只是你提供的看起来像一个可靠的工作例子。我一直在使用该原型,从未遇到过您面临的问题
-
我想说只运行一个
mvn clean package。从target中取出.war并将其放入Tomcatwebapp。删除任何旧的战争(爆炸的战争)。启动 Tomcat。然后查看爆炸的战争,在 webapps 里面,确保类在 WEB-INF/classes 中 -
@peeskillet 好的。我还需要发布什么来解决这个问题?我只是想通过youtube.com/… 完成本教程,因为我已经安装了所有新东西。
-
就是这样。我不知道。您提供的信息看起来不错。我唯一能想到的是,当你启动 Tomcat 时,
.war会爆炸成一个与.war同名的目录。查看该目录,然后打开WEB-INF/classes/org/test/messanger并确保MessageResource.class在那里。