首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >org.apache.camel.InvalidPayloadException:当unMarshalling Jaxb对象出现错误时,没有可用类型的主体被抛出

org.apache.camel.InvalidPayloadException:当unMarshalling Jaxb对象出现错误时,没有可用类型的主体被抛出
EN

Stack Overflow用户
提问于 2013-10-04 05:07:57
回答 3查看 21.1K关注 0票数 3

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

代码语言:javascript
复制
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来阅读这篇文章。

代码语言:javascript
复制
    <camel:route id="myRoute">
        <camel:from uri="RabbitMQEndpoint" />
        <camel:to uri="bean:MyHandler" />
    </camel:route>

处理程序正在使用在camel端重做的Jaxb对象

代码语言:javascript
复制
 @Handler
 public void handleRequest(MyDTO dto) throws ParseException {

我得到了我没有预料到的错误。

代码语言:javascript
复制
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)

非常感谢您的解决方案。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-04 16:15:38

它是Jaxb对象。我的印象是,如果我移动出版商太骆驼。事情会解决的。

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

代码语言:javascript
复制
    @EndpointInject(context = "myContext" , uri = "direct:myRoute")
    private ProducerTemplate sendAMyContext;

在我调用的方法中

代码语言:javascript
复制
     @Override
public MyResponse putMyCall(
        MyRequest myrequest) {

        sendMyContext.sendBody(myrequest);

我的骆驼路线很简单

代码语言:javascript
复制
    <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对象转换为字符串的处理程序(因为出错)

代码语言:javascript
复制
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();

  }

仍然是同样的错误

代码语言:javascript
复制
 [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

我的接收骆驼路线如下

代码语言:javascript
复制
<camel:route id="processVDelivery">
            <camel:from uri="aVEndpoint" />
            <camel:to uri="bean:myDataHandler" />
        </camel:route>

我添加了

代码语言:javascript
复制
    <camel:convertBodyTo type="String"></camel:convertBodyTo>

到它,它给我一个错误,它不能把它从字符串转换成对象

添加这个

代码语言:javascript
复制
    <camel:unmarshal></camel:unmarshal>



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

问题的答案是所有mashaller都必须是类路径的一部分

代码语言:javascript
复制
---------------------------------


    <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自定义封送处理程序

代码语言:javascript
复制
<camel:dataFormats>

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

    </camel:dataFormats

确保将jaxb.index文件保存在根级别为Jaxb对象名的com.something包中

票数 1
EN

Stack Overflow用户

发布于 2013-10-04 15:04:02

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

来自Rabbit的byte[]数据是XML格式的吗?您是否有关于MyDTO类的JAXB注释,因此您可以使用JAXB将其从xml编组到Java?

票数 3
EN

Stack Overflow用户

发布于 2021-12-16 18:36:51

在Spring boot中使用Camel Rest

代码语言:javascript
复制
 rest("/authenticate")
                .post()
                .route()
                .setHeader("Content-Type", constant("application/json"))
                .setHeader("Accept", constant("application/json"))
                .setHeader(Exchange.HTTP_METHOD, constant("POST"))
                  .process(exchange -> {
        org.apache.camel.TypeConverter tc = exchange.getContext().getTypeConverter();
String str_value = tc.convertTo(String.class, exchange.getIn().getBody());
             System.err.println(str_value);
              exchange.getOut().setBody(str_value);
      })
                //.removeHeader(Exchange.HTTP_URI)
                .to("http://localhost:8082/authenticate?bridgeEndpoint=true")
                  //.convertBodyTo(DTO.class)
                .log("Boo!")
                .end()
                .endRest();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19169267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档