Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this simple example of JSON marshalling in Apache Camel work?

I've spent quite a while trying to figure this out. I am working on writing a service which receives a user name and password. It then uses a processor to generate an authentication token which is returned in the Out part of the message. I want to accept JSON formatted parameters, and am trying to get type conversion to work correctly. I've reduced the problem to a self contained unit test which is below:

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;
    }

}

}

When I run this test, I get this exception in the console:

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>

Am I approaching this wrong somehow? Please Help!

like image 795
Spina Avatar asked Feb 20 '23 09:02

Spina


1 Answers

I found one way to make this work. I switched to Jackson as my JSON parser and it worked. To do this, all I had to do was change the RouteBuilder to this:

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

I also had to change the format of the JSON being sent over the wire from this:

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

to this (which I like more anyway):

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

like image 73
Spina Avatar answered Feb 23 '23 00:02

Spina