gpt4 book ai didi

java - Jersey 2.0 - Glassfish 4 Actor 异常

转载 作者:行者123 更新时间:2023-12-01 13:19:11 24 4
gpt4 key购买 nike

我正在尝试部署一个简单的应用程序,该应用程序使用 jersey 和 glassfish 4 显示 Restful 服务的状态。我收到以下异常:

org.glassfish.jersey.internal.ServiceConfigurationError: 
org.glassfish.jersey.internal.spi.AutoDiscoverable: The class
org.glassfish.jersey.server.internal.monitoring.MonitoringAutodiscoverable implementing
provider interface org.glassfish.jersey.internal.spi.AutoDiscoverable could not be
instantiated: Cannot cast
org.glassfish.jersey.server.internal.monitoring.MonitoringAutodiscoverable to
org.glassfish.jersey.internal.spi.AutoDiscoverable

我的 web.xml 看起来像这样

<web-app>
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

我的服务如下所示:

@Path(MyServices.SERVICE_URL)
public class MyServices
{
@GET
@Produces("text/html")
public String getStatus()
{
return "My service is running.";
}
}

有人知道我在这里做错了什么吗?

编辑我将此类添加到我的 war 项目中:

@ApplicationPath("/*")
public class MyApplication extends ResourceConfig
{
public MyApplication()
{
packages("com.java.services");
}
}

我仍然遇到同样的异常:

javax.servlet.ServletException: Servlet.init() for servlet com.java.services.MyApplication threw exception

最佳答案

GlassFish 4.0 是一个 Servlet 3.x 容器,因此您必须更改 deployment model

<!-- Servlet declaration can be omitted in which case
it would be automatically added by Jersey -->
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>

<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>

但即使这个 web.xml 设置也不再需要,只需实现 Application带有 @ApplicationPath 注释的子类一切都会按预期进行

@ApplicationPath("resources")
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("org.foo.rest;org.bar.rest");
}
}
<小时/>

结果

Result

关于java - Jersey 2.0 - Glassfish 4 Actor 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22184801/

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