我花了很长时间试图弄清楚这一点。我正在编写一个接收用户名和密码的服务。然后,它使用处理器生成身份验证令牌,该令牌在消息的 Out 部分中返回。我想接受 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>
我是否以某种方式接近这个错误?请帮忙!