我是JAX-RS注释的新手,当我尝试运行此命令时,出现了类似
的错误java.lang.ClassNotFoundException:org.glassfish.jersey.servlet.ServletContainer,
帮助我解决此问题,我添加了记录日志的1.2 jar和jersey-bundle 1.18 jar。
index.html
<a href="rest/hello/javatpoint">Click Here</a>
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">
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.javatpoint.rest</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>
HelloService.java
package com.javatpoint.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/hello")
public class HelloService{
@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg) {
String output = "Jersey say : " + msg;
return Response.status(200).entity(output).build();
}
}
答案 0 :(得分:0)
使用此web.xml servlet指令-更改必要的信息:
<servlet> <servlet-name>Jersey REST Service</servlet-name> <!-- Define ServletContainer of Jersey --> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <!-- Define the package to search for classes --> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.javatpoint.rest</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>