我正在尝试向Paty Web OSGi容器中嵌入的Jetty 8注册一个自定义错误处理程序。我将以下基本xml配置添加到jetty.xml文件中:
<Call name="addBean">
<Arg>
<New class="my.custom.handler">
<Set name="server"><Ref id="Server"/></Set>
</New>
</Arg>
</Call>
但是,在此配置Jetty无法启动后,我从日志文件中收到以下根本原因的错误消息:
Caused by: java.lang.NoSuchMethodException: class my.error.handler.MyErrorHandler.setServer(class org.ops4j.pax.web.service.jetty.internal.JettyServerWrapper)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.set(XmlConfiguration.java:592)[70:org.eclipse.jetty.aggregate.jetty-all-server:8.1.19.v20160209]
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:396)[70:org.eclipse.jetty.aggregate.jetty-all-server:8.1.19.v20160209]
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.newObj(XmlConfiguration.java:827)[70:org.eclipse.jetty.aggregate.jetty-all-server:8.1.19.v20160209]
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.itemValue(XmlConfiguration.java:1141)[70:org.eclipse.jetty.aggregate.jetty-all-server:8.1.19.v20160209]
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.value(XmlConfiguration.java:1044)[70:org.eclipse.jetty.aggregate.jetty-all-server:8.1.19.v20160209]
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.call(XmlConfiguration.java:735)[70:org.eclipse.jetty.aggregate.jetty-all-server:8.1.19.v20160209]
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:400)[70:org.eclipse.jetty.aggregate.jetty-all-server:8.1.19.v20160209]
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:334)[70:org.eclipse.jetty.aggregate.jetty-all-server:8.1.19.v20160209]
at org.eclipse.jetty.xml.XmlConfiguration.configure(XmlConfiguration.java:281)
... 16 more
似乎Pax使用JettyServerWrapper包装Jetty的Server类,并且无法使用来自xml配置的Reflection设置它。当我深入研究Jetty源代码时,我发现此代码与xml:
中的反射配置有关来自org.eclipse.jetty.xml.XmlConfiguration的内部类JettyXmlConfiguration:
private void set(Object obj, XmlParser.Node node) throws Exception
{
...
// Try for trivial match
try
{
Method set = oClass.getMethod(name,vClass);
set.invoke(obj,arg);
return;
}
...
}
从代码中可以明显看出,XmlConfiguration的set方法只是尝试获取具有特定类型的方法,但它并不寻求父类。这就是无法在自定义错误处理程序上设置Pax包装的Server实例的原因。
因此,使用xml配置Jetty变得不可用。你有任何解决方案吗?
答案 0 :(得分:0)
您可以在文档中找到有关如何配置Jetty的说明: http://ops4j.github.io/pax/web/SNAPSHOT/User-Guide.html#adding-specialized-contexthandlers
此外,这里有一个快速总结:
<Get name="handler">
<Call name="addHandler">
<Arg>
<New class="org.eclipse.jetty.servlet.ServletContextHandler">
<Set name="contextPath">/app</Set>
<Set name="resourceBase">/path/to/your/share</Set>
<Call name="addServlet">
<Arg>org.eclipse.jetty.servlet.DefaultServlet</Arg>
<Arg>/</Arg>
</Call>
</New>
</Arg>
</Call>
</Get>
您不需要以定义如何开始的方式配置Jetty,而是需要适应&#34;正在运行的实例。