org.apache.camel.InvalidPayloadException:unMarshalling Jaxb对象时没有抛出类型错误的正文

时间:2013-10-03 21:07:57

标签: jaxb rabbitmq apache-camel

我通过Java将JAXB对象发送到Rabbit MQ。

JAXBContext jaxbContext = JAXBContext.newInstance(MyDTO.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    // output pretty printed
    jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
     java.io.StringWriter sw = new StringWriter();
     jaxbMarshaller.marshal(deliveryrequest, sw);

    ConnectionFactory factory = new ConnectionFactory() ;

    //TODO change the hardcoding to the properties file
    factory.setHost("rabbitmq.host.net");
    factory.setUsername("user");
    factory.setPassword("pass");
    Channel channel ;
    Connection connection;

    Map<String, Object> args = new HashMap<String, Object>(); 
    String haPolicyValue = "all"; 
    args.put("x-ha-policy", haPolicyValue); 


    connection = factory.newConnection();
    channel = connection.createChannel();
    //TODO change the hardcoding to the properties file
    channel.queueDeclare("upload.com.some.queue", true, false, false, args);
    //TODO change the hardcoding to the properties file
    channel.basicPublish("com.some.exchange", "someroutingKey", 
            new AMQP.BasicProperties.Builder().contentType("text/plain")
            .deliveryMode(2).priority(1).userId("guest").build(),
            sw.toString().getBytes());

我在不同的应用程序中使用Camel来阅读它。

    <camel:route id="myRoute">
        <camel:from uri="RabbitMQEndpoint" />
        <camel:to uri="bean:MyHandler" />
    </camel:route>

处理程序正在使用骆驼侧重做的Jaxb对象

 @Handler
 public void handleRequest(MyDTO dto) throws ParseException {

我收到的错误是我没想到的。

Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: byte[] to the required type: com.mycompany.MyDTO with value [B@1b8d3e42
at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:181)
at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:99)

感谢您的解决方案。

2 个答案:

答案 0 :(得分:3)

来自rabbit的邮件正文类型是一个byte [],并且您想要调用一个作为MyDTO类型的bean。您的类型不匹配。并且Camel找不到可以将消息体从byte []转换为MyDTO类型的类型转换器。

Rabbit的byte []数据是否为XML格式?你是否在MyDTO类上有JAXB注释,所以你可以使用JAXB来编组从xml到Java的那些?

答案 1 :(得分:1)

是Jaxb对象。我的印象是,如果我移动出版商太骆驼。事情将被整理出来。

我将发布商更改为以下内容。

    @EndpointInject(context = "myContext" , uri = "direct:myRoute")
    private ProducerTemplate sendAMyContext;

在方法中我打电话

     @Override
public MyResponse putMyCall(
        MyRequest myrequest) {

        sendMyContext.sendBody(myrequest);

我的骆驼路线很简单

    <camel:camelContext id="MyContext" autoStartup="true" xmlns="http://camel.apache.org/schema/spring">


    <camel:endpoint id="myQueue" uri="${my.queue.1}" />

        <camel:route>
        <camel:from uri="direct:myRoute"/>
        <camel:to uri="bean:mySendHandler"/>
        <camel:convertBodyTo type="String"></camel:convertBodyTo>
        <camel:to uri="ref:myQueue" />
    </camel:route>

</camel:camelContext>

我添加了将Jaxb对象转换为字符串的处理程序(因为错误)

public class MySendHandler {



 @Handler
 public String myDelivery( MyRequest myRequest) throws ParseException, JAXBException {


         JAXBContext jaxbContext = JAXBContext.newInstance(MyRequest.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        java.io.StringWriter sw = new StringWriter();
        jaxbMarshaller.marshal(attributedDeliveryRequest, sw);

     return sw.toString();

  }

仍然是同样的错误

 [SpringAMQPConsumer.SpringAMQPExecutor-1] WARN  org.springframework.amqp.support.converter.JsonMessageConverter (JsonMessageConverter.java:111) - Could not convert incoming message with content-type [text/plain]
SpringAMQPConsumer.SpringAMQPExecutor-1] ERROR org.apache.camel.processor.DefaultErrorHandler (MarkerIgnoringBase.java:161) - Failed delivery for (MessageId: ID-Con ExchangeId: ID-65455-1380873539452-3-2). 
Exhausted after delivery attempt: 1 caught: org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[Message: <XML>]
org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[Message: <xml>
    .....
Caused by: org.apache.camel.InvalidPayloadException: No body available of type: MyDTO but has value: [B@7da255c of type: byte[] on: Message: <xml>
. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: byte[] to the required type: MyDeliveryDTO with value [B@7da255c]
    at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:101)
    at org.apache.camel.builder.ExpressionBuilder$38.evaluate(ExpressionBuilder.java:934)
    ... 74 more
Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: byte[] to the required type: MyDTO with value [B@7da255c
    at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:181)
    at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:99)
    ... 75 more

我收到的骆驼路线如下

<camel:route id="processVDelivery">
            <camel:from uri="aVEndpoint" />
            <camel:to uri="bean:myDataHandler" />
        </camel:route>

我添加了

    <camel:convertBodyTo type="String"></camel:convertBodyTo>

它给了我错误,它无法将它从字符串转换为对象

添加此

    <camel:unmarshal></camel:unmarshal>



-------------------------

问题的答案将是你的所有mashaller必须成为classpath的一部分

---------------------------------


    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-jaxrs</artifactId>
        <version>${jackson.version}</version>
    </dependency>

-------您也可以使用dataformat

进行自定义编组
<camel:dataFormats>

                   <camel:jaxb id="name" contextPath="com.something"/>

    </camel:dataFormats

确保将com.something包中的jaxb.in​​dex文件与根级别Jaxb对象名称保存在一起