Spring-WS:指定内容类型
我有一个基于 AbstractJDomPayloadEndpoint
的 Spring Webservice。该服务工作正常,只是我的客户端需要将 HTTP 标头 Content-Type
设置为正确的字符集(在我的例子中为 utf-8)。我找不到在哪里可以配置它。
我尝试编写一个简单的 servlet Filter
:
chain.doFilter(request, response);
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setHeader("Content-Type", "text/xml; charset=utf-8");
但这根本不会改变标头。我怀疑内容类型标头是由 Spring-WS 设置的,并且响应已提交,因此我在过滤器中设置的任何内容都不会产生影响。
我的应用程序服务器是WebLogic 9.2.3。
I have a Spring Webservice based on AbstractJDomPayloadEndpoint
. This service works fine, except that my client needs the HTTP header Content-Type
to be set to the right charset (utf-8 in my case). I cant find where I can configure that.
I tried writing a simple servlet Filter
:
chain.doFilter(request, response);
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setHeader("Content-Type", "text/xml; charset=utf-8");
But this doesnt change the headers at all. I suspect that the content type header is set by Spring-WS, and the response is commited, so nothing I set in a filter will have an impact.
My appserver is WebLogic 9.2.3.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您的过滤器代码将会失败,因为当
doFilter()
完成时,响应将已完全提交,并且您将无法更改内容类型标头。我建议编写
HttpServletResponseWrapper
的子类,重写setContentType()
和/或setCharacterEncoding()
方法以将值设置为您想要的值。然后,您将包装器的实例(包装原始响应)传递给doFilter()
。Yes, your filter code will fail because by the time
doFilter()
completes, the response will have been fully committed, and you won't be allowed to change the content type header.I suggest writing a subclass of
HttpServletResponseWrapper
, overriding thesetContentType()
and/orsetCharacterEncoding()
methods to set the value to the one you want. You then pass the instance of the wrapper (which wraps the original response) to thedoFilter()
.