我在应用程序中使用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$1.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中添加自定义标头时,可能会导致这种情况。初始化对应的映射时,应使用Series
的映射(在Restlet中为Parameter
),而不是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"));
希望对您有帮助,
蒂埃里