小编Zac*_*ach的帖子

Camel Rest DSL响应编码

我目前正在使用新的Camel REST DSL作为基础,开发基于REST的Java应用程序.它主要工作,除了我注意到通过REST客户端(而不是说浏览器)调用URL时,JSON响应是"乱码",并且我认为是错误的编码


MyRouteBuilder.java

@Component
public class MyRouteBuilder extends RouteBuilder{
    @Autowired
    LocalEnvironmentBean environmentBean;

    @Override
    public void configure() throws Exception {
        restConfiguration().component("jetty").host("0.0.0.0").port(80)
            .bindingMode(RestBindingMode.auto);

        rest("/testApp")
            .get("/data").route()
                .to("bean:daoService?method=getData")
                .setProperty("viewClass", constant(CustomeJsonViews.class))
                .marshal("customDataFormat").endRest()
            .get("/allData").route()
                .to("bean:daoService?method=getDatas")
                .setProperty("viewClass", constant(CustomeJsonViews.class))
                .marshal("customDataFormat").endRest();
    }
}
Run Code Online (Sandbox Code Playgroud)

CustomeDataFormat.java

public class CustomDataFormat  implements DataFormat{
    private ObjectMapper jacksonMapper;
    public CustomDataFormat(){
        jacksonMapper = new ObjectMapper();
    }
    @Override
    public void marshal(Exchange exchange, Object obj, OutputStream stream) throws Exception {
        Class view = (Class) exchange.getProperty("viewClass");
        if (view != null)
        {
            ObjectWriter w = jacksonMapper.writerWithView(view);
            w.writeValue(stream, obj);
        } …
Run Code Online (Sandbox Code Playgroud)

java rest json apache-camel jackson

2
推荐指数
1
解决办法
3675
查看次数

标签 统计

apache-camel ×1

jackson ×1

java ×1

json ×1

rest ×1