如何诊断 Restlet 2.3.5 中的 "org.restlet.data.Parameter cannot be cast to org.restlet.data.Header" 错误?

How to diagnose "org.restlet.data.Parameter cannot be cast to org.restlet.data.Header" error in Restlet 2.3.5?

我在我的应用程序中使用 Restlet 2.3.5。当调用某个服务器资源的 GET 请求处理程序时,出现以下错误:

[10:26:04] [Restlet-860541310/WARN]: Nov 29, 2015 10:26:04 AM org.restlet.engine.adapter.ServerAdapter addResponseHeaders
WARNING: Exception intercepted while adding the response headers
java.lang.ClassCastException: org.restlet.data.Parameter cannot be cast to org.restlet.data.Header
    at org.restlet.engine.header.HeaderUtils.addExtensionHeaders(HeaderUtils.java:226)
    at org.restlet.engine.header.HeaderUtils.addResponseHeaders(HeaderUtils.java:653)
    at org.restlet.engine.adapter.ServerAdapter.addResponseHeaders(ServerAdapter.java:83)
    at org.restlet.engine.adapter.ServerAdapter.commit(ServerAdapter.java:184)
    at org.restlet.engine.adapter.HttpServerHelper.handle(HttpServerHelper.java:144)
    at org.restlet.engine.connector.HttpServerHelper.handle(HttpServerHelper.java:64)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
    at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:80)
    at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:677)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
    at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:649)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

问题是这个异常是在我编写的代码之外抛出的(我在我的服务器资源中添加了日志语句,根据它们,异常是在其他地方抛出的)。

结果,我收到 500 响应(内部服务器错误),即使我的服务器资源将正确的数据发送回客户端。

我怎样才能找出导致此错误的确切原因?

这可能是在尝试在 Restlet 中添加自定义 headers 时引起的。在初始化相应的映射时,您将使用Parameter的映射(Restlet中的Series)而不是Header...

你可以使用类似的东西:

Series<Header> responseHeaders = (Series<Header>) 
response.getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS); 
if (responseHeaders == null) { 
    responseHeaders = new Series(Header.class); 
    response.getAttributes().put(
            HeaderConstants.ATTRIBUTE_HEADERS, responseHeaders); 
}
responseHeaders.add(new Header("X-MyHeader", "value")); 

希望对你有帮助, 蒂埃里