English 中文(简体)
为什么JSON在阿帕奇骆驼中 拼凑的简单例子不起作用呢?
原标题:Why doesn t this simple example of JSON marshalling in Apache Camel work?

我花了相当长的一段时间试图弄明白这一点。 我正在努力撰写一个收到用户名和密码的服务。 然后它会使用一个处理器生成一个验证符, 并在信件的“ 输出” 部分中返回。 我想接受 JSON 格式化的参数, 并试图让类型转换正确工作 。 我将问题简化为一个自定义的单位测试, 具体如下:

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonDataFormat;
import org.apache.camel.model.dataformat.JsonLibrary;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
import com.thoughtworks.xstream.annotations.XStreamAlias;


public class BasicJsonMarshallingTest extends CamelTestSupport {

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    final Processor simpleProcessor = new Processor() {
        @Override public void process(Exchange exchange) throws Exception {
            SimpleBean bean = exchange.getIn().getBody(SimpleBean.class);
            if(bean == null){
                return;
            }
            exchange.getOut().setBody("a=" + bean.getA() + " b=" + bean.getB());
        }
    };

    return new RouteBuilder() {
        @Override public void configure() throws Exception {
            JsonDataFormat jsonFormat = new JsonDataFormat(JsonLibrary.XStream);
            jsonFormat.setUnmarshalType(SimpleBean.class);
            from("direct:service").unmarshal(jsonFormat).process(simpleProcessor);
        }
    };
}

@Test
public void testSuccessfulAuthentication(){
    Exchange lAuthRequest = createExchangeWithBody("{"simple":{"a":"v1","b":"v2"}}");
    template.send("direct:service", lAuthRequest);
    assertEquals("a=v1 b=v2", lAuthRequest.getOut().getBody());
}

@XStreamAlias("simple")
public static final class SimpleBean {

    private String a;
    private String b;

    public void setA(String a) {
        this.a = a;
    }
    public String getA() {
        return a;
    }
    public void setB(String b) {
        this.b = b;
    }
    public String getB() {
        return b;
    }

}

}

当我做这个测试时,我在控制台得到这个例外:

com.thoughtworks.xstream.mapper.CannotResolveClassException: simple
    at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:56)[xstream-1.4.1.jar:]
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)[xstream-1.4.1.jar:]
    <snip>

我是不是有点搞错了?

最佳回答

我找到一个方法来做这个工作。我转换到杰克逊,当我的JSON探险家,它成功了。要做到这一点,我要做的就是把路由改成这个:

    return new RouteBuilder() {
        @Override public void configure() throws Exception {
            from("direct:service").unmarshal().json(JsonLibrary.Jackson, SimpleBean.class).process(simpleProcessor);
        }
    };

我还不得不改变JSON被发送到电线上的格式,

{“ 简单 } { “ a ” : “ v1 ”, b : “ v2 ” {{ “ a ” : “ v1 ”, b : “ v2 ” {

至於这个(我更喜欢这个),

{"a":"v1","b":"v2"}

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签