Servlets 使用JAX-RS和Jetty时出错?
我正在尝试创建一个简单的REST api,并遵循Jersey网站上的教程: 以下是错误: 原因:javax.servlet.UnavailableException:holder中没有类 位于org.eclipse.jetty.servlet.BaseHolder.doStart(BaseHolder.java:88) 位于org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:361) 位于org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) 位于org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:874) web.xmlServlets 使用JAX-RS和Jetty时出错?,servlets,jakarta-ee,jetty,Servlets,Jakarta Ee,Jetty,我正在尝试创建一个简单的REST api,并遵循Jersey网站上的教程: 以下是错误: 原因:javax.servlet.UnavailableException:holder中没有类 位于org.eclipse.jetty.servlet.BaseHolder.doStart(BaseHolder.java:88) 位于org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:361) 位于org.eclipse.j
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<servlet>
<display-name>My JAX-RS Servlet</display-name>
<servlet-name>MyJaxRsApp</servlet-name>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.example.flexible.helloworld.MyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyJaxRsApp</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
应用程序“根”
package com.example.flexible.helloworld;
导入java.util.HashSet;
导入java.util.Set;
导入javax.ws.rs.ApplicationPath;
导入javax.ws.rs.core.Application;
公共类MyApplication扩展了应用程序{
@凌驾
public Set>s=新哈希集
org.glassfish.jersey.core
泽西服务器
2.23.2
${project.build.directory}/${project.build.finalName}/WEB-INF/classes
com.google.cloud.tools
appengine maven插件
${appengine.maven.plugin}
真的
真的
org.eclipse.jetty
jetty maven插件
${jetty maven插件版本}
org.apache.maven.plugins
maven战争插件
3.0.0
实际上,我构建了这个Google AppEngine helloworld的ontop示例,它使用jetty,所以我部署它的方式是“mvn clean jetty:run exploded”。据我所知,jetty是一个web服务器/servlet容器。但我这里有一个servlet吗?是隐式创建的吗
另外,我查找了错误,发现需要定义
。泽西岛的文档中也提到了这一点。但是当我添加行org.glassfish.jersey.servlet.ServletContainer
时,我在localhost:8080/helloworld上找到了资源。另外,servlet 3.0不需要定义
(jersey的文档中也提到了)。在没有web.xml的情况下设置JAX-RS应用程序配置
使用ResourceConfig(不需要web.xml)
@javax.ws.rs.ApplicationPath(ResourcePath.API\u ROOT)
公共类应用程序配置扩展了ResourceConfig{
公共应用程序配置(){
//注册客户端所需的必要头文件
寄存器(CORSConfigurationFilter.class);
//jackson特性和提供程序用于对象序列化
寄存器(JacksonFeature.class);
注册(JacksonProvider.class);
//使用包注入并注册所有资源类
包(“com.example.myapi.package”);
}
@凌驾
公共集合getPropertyNames(){
返回super.getPropertyNames();
}
}
不能混合发现(字节码注释扫描)和声明(WEB-INF/WEB.xml)相同端点(servlet/filters)并期望其工作。请使用其中一种。
package com.example.flexible.helloworld;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Application;
public class MyJaxRsApplication {
/**
* 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
@Path("helloworld")
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return "Got it!";
}
}
package com.example.flexible.helloworld;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(MyJaxRsApplication.class);
return s;
}
}
<!-- [START project] -->
<project>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<groupId>org.myproject</groupId>
<artifactId>myproject-0.0</artifactId>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<appengine.maven.plugin>0.1.1-beta</appengine.maven.plugin>
<jetty-maven-plugin-version>9.3.7.v20160115</jetty-maven-plugin-version>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<!-- [START dependencies] -->
<dependencies>
<!-- REST framework -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.23.2</version>
</dependency>
</dependencies>
<!-- [END dependencies] -->
<build>
<!-- for hot reload of the web application -->
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>${appengine.maven.plugin}</version>
<configuration>
<!-- deploy configuration -->
<deploy.promote>true</deploy.promote>
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-maven-plugin-version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>
</project>
<!-- [END project] -->
@javax.ws.rs.ApplicationPath(ResourcePath.API_ROOT)
public class ApplicationConfig extends ResourceConfig {
public ApplicationConfig() {
//register the necessary headers files needed from client
register(CORSConfigurationFilter.class);
//The jackson feature and provider is used for object serialization
register(JacksonFeature.class);
register(JacksonProvider.class);
//inject and registered all resources class using the package
packages("com.example.myapi.package");
}
@Override
public Collection<String> getPropertyNames() {
return super.getPropertyNames();
}
}