我试图让我的第一个Jersey Web服务项目正常工作,但出现此错误Jersey: The requested resource is not available.
我已经通过eclispe Maven安装了Jersey 2.16
并安装了Tomcat 8.0.21
我在scr/main/java - org.test.messanger
里面放了MessageResource.java类
我正在输入此链接:http://localhost:8080/messanger/webapi/messages
如果我点击此链接http://localhost:8080/messanger/webapi/myresource
,我将得到Got it!
我用以下数据将Jersy和maven一起添加:
org.glassfish.jersey.archetypes
泽西-快速入门-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>
最佳答案
做一件事。
从target / messenger.jar复制messenger.jar并将其粘贴到位置TOMCAT_DIR / tomcat / webapps的tomcat文件夹中。
TOMCAT_DIR是您本地的tomcat的目录位置。
现在转到TOMCAT_DIR / tomcat / bin并启动startup.bat或startup.sh。
现在打开URL http://localhost:8080/messanger/webapi/messages。这应该工作。
如果可行,请从eclipse中删除已配置的tomcat服务器,然后再配置一次。