gpt4 book ai didi

java - Servlet 类 com.foobar.jaxrs.application.MyApplication 不是 javax.servlet.Servlet

转载 作者:行者123 更新时间:2023-11-30 06:57:44 24 4
gpt4 key购买 nike

我正在尝试使用 Servlet 3.1 和应用程序子类配置 Jersey。已经阅读了一段时间的文档并试图让它继续下去,但我不确定这里出了什么问题。

web.xml(虽然我不需要一个,但没有一个我会得到一个 404...)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">

<display-name>Foobar Models</display-name>

<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>

<session-config>
<session-timeout>30</session-timeout>
<cookie-config>
<name>SESSIONID</name>
</cookie-config>
</session-config>

<servlet>
<servlet-name>Foo Bar Application</servlet-name>
<servlet-class>com.foobar.jaxrs.application.FooBarApplication</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Foo Bar Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>

</web-app>

应用子类

package com.foobar.jaxrs.application;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/api")
public class FooBarApplication extends Application {
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(com.foobar.api.HealthCheckResource.class);
return s;
}
}

健康检查资源.java

package com.foobar.api;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("health")
public class HealthCheckResource {

@GET
@Produces("text/html")
public String getHeath() {
return "Foo Bar Application is healthy!";
}

}

在 jetty 中运行(在 Tomcat 8 中相同)

HTTP ERROR 404

Problem accessing /foobar/api/health. Reason:

Servlet class com.foobar.jaxrs.application.FooBarApplication is not a javax.servlet.Servlet
Caused by:

javax.servlet.UnavailableException: Servlet class com.foobar.jaxrs.application.FooBarApplication is not a javax.servlet.Servlet
at org.mortbay.jetty.servlet.ServletHolder.checkServletType(ServletHolder.java:362)
at org.mortbay.jetty.servlet.ServletHolder.doStart(ServletHolder.java:243)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:685)
....

最佳答案

好吧,消息说明了一切。您正在尝试将您的 Application 子类部署为 servlet,它不是从 Servlet 扩展而来的。这不可能行得通。

这不是怎么来的the Jersey documentation告诉做事。这是它说的:

Hooking up Jersey as a Servlet

<web-app>
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
...
</init-param>
</servlet>
...
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/myApp/*</url-pattern>
</servlet-mapping>
...
</web-app>

请注意,servlet 类是 org.glassfish.jersey.servlet.ServletContainer。不是您的自定义应用程序子类。

Custom Application subclass

If you extend the Application class to provide the list of relevant root resource classes (getClasses()) and singletons (getSingletons()), i.e. your JAX-RS application model, you then need to register it in your web application web.xml deployment descriptor using a Servlet or Servlet filter initialization parameter with a name of javax.ws.rs.Application [sic] as follows:

Example 4.11. Configuring Jersey container Servlet or Filter to use custom Application subclass

<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>org.foo.MyApplication</param-value>
</init-param>

请注意,自定义应用程序子类被配置为 servlet 的初始参数。不是作为 servlet 类。

关于java - Servlet 类 com.foobar.jaxrs.application.MyApplication 不是 javax.servlet.Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33323138/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com