Spring-WS:指定Content-Type

问题描述:

我有一个基于 AbstractJDomPayloadEndpoint 的Spring Web服务。此服务工作正常,但我的客户端需要将HTTP标头 Content-Type 设置为正确的字符集(在我的情况下为utf-8)。我无法找到我可以配置的地方。

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.

我尝试编写一个简单的servlet 过滤器

I tried writing a simple servlet Filter :

chain.doFilter(request, response);
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setHeader("Content-Type", "text/xml; charset=utf-8");

但这根本不会改变标题。我怀疑内容类型标头是由Spring-WS设置的,并且响应是提交的,所以我在过滤器中设置的任何内容都不会产生影响。

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.

我的appserver是WebLogic 9.2.3。

My appserver is WebLogic 9.2.3.

是的,您的过滤器代码将失败,因为时间 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.

我建议写一个 HttpServletResponseWrapper 的子类,重写 setContentType()和/或 setCharacterEncoding()将值设置为所需值的方法。然后,将包装器的实例(包装原始响应)传递给 doFilter()

I suggest writing a subclass of HttpServletResponseWrapper, overriding the setContentType() and/or setCharacterEncoding() methods to set the value to the one you want. You then pass the instance of the wrapper (which wraps the original response) to the doFilter().