简体   繁体   English

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

[英]Servlet class com.foobar.jaxrs.application.MyApplication is not a javax.servlet.Servlet

I'm trying to configure Jersey with Servlet 3.1 and an Application Subclass. 我正在尝试使用Servlet 3.1和Application子类配置Jersey。 Been reading documentation for a while and trying to get this going, but i'm not sure what's wrong here. 一段时间以来一直在阅读文档,并试图解决这个问题,但是我不确定这里出了什么问题。

web.xml (though I shouldn't need one I get a 404 without one...) 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>

Application Subclass 应用子类

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;
}
}

HealthCheckResource.java HealthCheckResource.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!";
    }

}

Running in jetty (same in Tomcat 8) 在码头上运行(与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)
....

Well, the message says it all. 好吧,消息说明了一切。 You're trying to deploy your Application subclass, which doesn't extend from Servlet, as a servlet. 您正在尝试将不从Servlet扩展的Application子类部署为Servlet。 That can't possibly work. 那可能行不通。

This is not how the Jersey documentation tells to do things. 这不是Jersey文档指示执行操作的方式。 Here is what it says: 它说的是:

Hooking up Jersey as a Servlet 将Jersey连接为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>

Note that the servlet class is org.glassfish.jersey.servlet.ServletContainer . 注意,该Servlet类是org.glassfish.jersey.servlet.ServletContainer Not your custom Application subclass. 不是您的自定义Application子类。

Custom Application subclass 自定义应用程序子类

If you extend the Application class to provide the list of relevant root resource classes (getClasses()) and singletons (getSingletons()), ie 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: 如果扩展Application类以提供相关的根资源类(getClasses())和单例(getSingletons())的列表,即JAX-RS应用程序模型,则需要在Web应用程序web.xml部署中注册它使用名称为javax.ws.rs.Application [sic]的Servlet或Servlet过滤器初始化参数的描述符,如下所示:

Example 4.11. 示例4.11 Configuring Jersey container Servlet or Filter to use custom Application subclass 配置Jersey容器Servlet或Filter以使用自定义Application子类

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

Note that the custom Application subclass is configured as an init-param of the servlet. 请注意,自定义Application子类被配置为servlet的init-param。 Not as the servlet class. 不作为servlet类。

I figured out the issue - I was running in gradle's jetty plugin which uses servlet 2.5 but i'm deploying a servlet 3.1 app. 我发现了问题-我在gradle的jetty插件中运行,该插件使用servlet 2.5,但是我正在部署servlet 3.1应用程序。 I was able to get it to work without the web.xml after deploying to Tomcat 8 with the correct configuration. 在使用正确的配置部署到Tomcat 8之后,我能够在没有web.xml的情况下正常运行。

It still doesnt work with Jetty (need to get an updated version of jetty), but in Tomcat 8 this works: 它仍然不能与Jetty一起使用(需要获得Jetty的更新版本),但是在Tomcat 8中,它可以工作:

<?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>Foo Bar Models</display-name>

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

</web-app>

BaseApplication.java package com.foobar.jaxrs.application; BaseApplication.java包com.foobar.jaxrs.application;

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

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

@ApplicationPath("")
public class BaseApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(com.patrickkee.resources.HealthCheckResource.class);
        return s;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用SpringBootServletInitializer:应用程序不是javax.servlet.Servlet - Using SpringBootServletInitializer: Application is not a javax.servlet.Servlet UnavailableException: Servlet class 不是带有 Jetty 的 javax.servlet.Servlet - UnavailableException: Servlet class is not a javax.servlet.Servlet with Jetty RepositoryRestMvcConfiguration无法强制转换为javax.servlet.Servlet - RepositoryRestMvcConfiguration cannot be cast to javax.servlet.Servlet 将Servlet:“ main”强制转换为javax.servlet.Servlet时出错 - Error casting servlet: “main” to javax.servlet.Servlet eclipse servlet java.lang.ClassNotFoundException:javax.servlet.Servlet - eclipse servlet java.lang.ClassNotFoundException: javax.servlet.Servlet Servlet类引用未实现接口javax.servlet.Servlet的“ org.springframework.web.servlet.DispatcherServlet” - servlet-class references to “org.springframework.web.servlet.DispatcherServlet” that does not implement interface javax.servlet.Servlet java.lang.ClassCastException:无法转换为javax.servlet.Servlet - java.lang.ClassCastException: cannot be cast to javax.servlet.Servlet Jboss7 EAP:UT010009:类型为org.springframework.web.servlet.DispatcherServlet类型的Servlet远程处理未实现javax.servlet.Servlet - Jboss7 EAP : UT010009: Servlet remoting of type class org.springframework.web.servlet.DispatcherServlet does not implement javax.servlet.Servlet java.lang.ClassCastException:org.glassfish.jersey.servlet.ServletContainer无法转换为javax.servlet.Servlet - java.lang.ClassCastException: org.glassfish.jersey.servlet.ServletContainer cannot be cast to javax.servlet.Servlet 尝试使用 Servlet 打包 REST App 时如何修复“无法转换为 javax.servlet.Servlet”错误 - How to fix 'cannot be cast to javax.servlet.Servlet' error while trying to package REST App with Servlet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM