我想在Camel环境中使用Jetty组件。这是我的 spring-config.xml 的摘录:
...
<bean id="webEnc" class="web.WebEnc" />
<camelContext>
<route>
<from uri="jetty:http://0.0.0.0/enc" />
<process ref="webEnc" />
</route>
</camelContext>
...
这是用于返回String
的代码:
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class WebEnc implements Processor{
@Override
public void process(Exchange exchange) throws Exception {
exchange.getOut().setBody("abcäöüß\"€一二三"); //the last three symbols are chinese for '123'
}
}
本地地址可以工作( http://127.0.0.1/enc ),但浏览器无法正确显示字符串(显示为 'abcà¤à¶àÜàŸ"â‚一二三')。我认为问题是一些编码。如何设置编码,例如“utf-8”?
我在这里找不到任何有关编码或字符集或其他内容的提示( http://camel.apache.org/jetty.html )像这样。
最佳答案
我认为您必须在输出消息中使用 utf-8 字符集设置 Content-Type header ,如下所示:
@Override
public void process(Exchange exchange) throws Exception {
exchange.getOut().setBody("abcäöüß\"€一二三"); //the last three symbols are chinese for '123'
exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "text/html; charset=utf-8");
}
关于spring - Camel : Jetty response encoding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30001261/