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

I spent a lot of time trying to figure it out. I am working on writing a service that gets a username and password. He then uses the processor to generate an authentication token, which is returned to the Out part of the message. I want to accept formatted JSON parameters, and I'm trying to get the correct type conversion. I reduced the problem to a standalone 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 somehow approaching this wrong? Please, help!

+4
source share
1 answer

I found one way to make this work. I switched to Jackson as a JSON parser and it worked. For this, I only needed to change the RouteBuilder:

  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 sent by post:

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

to this (which I like best anyway):

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

+8
source

Source: https://habr.com/ru/post/1413835/


All Articles