CannotResolveClassException while trying to transform xml string into java object

旧时模样 提交于 2021-01-28 07:45:37

问题


I am trying to develop a mule flow which takes a xml in the form of a string from a JMS queue and converts it into a POJO. I am using JAXB annotations to define the mappings between the xml and the class attributes. Below is the sample xml that is received on the JMS queue as a string.

<FCUBS_NOTIFICATION xmlns="http://fcubs.ofss.com/notify/NOTIF_UP_TRANSACTION">
   <FCUBS_NOTIF_HEADER>
      <MSGID>9132820000357947</MSGID>
   </FCUBS_NOTIF_HEADER>
</FCUBS_NOTIFICATION>

My POJO class is as below:

package com.bean;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="FCUBS_NOTIFICATION")
public class Notification {

    private String MSGID;

    public String getMSGID() {
        return MSGID;
    }

    @XmlElement(name="MSGID")
    public void setMSGID(String mSGID) {
        MSGID = mSGID;
    }   
}

I have a jaxb.index file in the com.bean package consisting of only one bean

Notification

My mule flow config file is as below:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:quartz="http://www.mulesoft.org/schema/mule/quartz" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/quartz http://www.mulesoft.org/schema/mule/quartz/current/mule-quartz.xsd">
    <spring:beans>
        <spring:bean name="myJaxb" class="javax.xml.bind.JAXBContext" factory-method="newInstance">
            <!-- colon-separated (:) list of package names where JAXB classes exist -->
            <spring:constructor-arg value="com.bean"/>
        </spring:bean>
    </spring:beans>
    <jms:activemq-connector name="Active_MQ" specification="1.1" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>
    <flow name="JMS-exampleFlow1" doc:name="JMS-exampleFlow1">
        <jms:inbound-endpoint queue="NotificationQueue" connector-ref="Active_MQ" doc:name="JMS"/>
        <logger message="Payload after picking message from queue is #[message.payload]" level="INFO" doc:name="Logger"/>
        <mulexml:xml-to-object-transformer returnClass="com.bean.Notification" doc:name="XML to Object"/>
        <logger level="INFO" doc:name="Logger" message="Payload after xml to object transformation is #[message.payload]"/>
    </flow>
</mule>

I am using Mule Studio for development and below is the error I get -

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Started app 'jms-example'                                +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
INFO  2013-11-27 16:19:44,225 [[jms-example].JMS-exampleFlow1.stage1.02] org.mule.api.processor.LoggerMessageProcessor: Payload after picking message from queue is <FCUBS_NOTIFICATION xmlns="http://fcubs.ofss.com/notify/NOTIF_UP_TRANSACTION"><FCUBS_NOTIF_HEADER><MSGID>9132820000357947</MSGID></FCUBS_NOTIF_HEADER></FCUBS_NOTIFICATION>
ERROR 2013-11-27 16:19:44,227 [[jms-example].JMS-exampleFlow1.stage1.02] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : FCUBS_NOTIFICATION (com.thoughtworks.xstream.mapper.CannotResolveClassException). Message payload is of type: String
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. FCUBS_NOTIFICATION (com.thoughtworks.xstream.mapper.CannotResolveClassException)
  com.thoughtworks.xstream.mapper.DefaultMapper:56 (null)
2. FCUBS_NOTIFICATION (com.thoughtworks.xstream.mapper.CannotResolveClassException). Message payload is of type: String (org.mule.api.transformer.TransformerMessagingException)
  org.mule.transformer.AbstractTransformer:139 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transformer/TransformerMessagingException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
com.thoughtworks.xstream.mapper.CannotResolveClassException: FCUBS_NOTIFICATION
    at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:56)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

Is there anything I am missing here ?

Thanks in advance.


回答1:


The serialization/deserialization in the xml-to-object-transformer is done through XStream, so JAXB won't matter at that point.

You would need an alias:

<mulexml:xml-to-object-transformer returnClass="com.bean.Notification" doc:name="XML to Object">
    <mulexml:alias name="FCUBS_NOTIFICATION" class="com.bean.Notification"/>
</mulexml:xml-to-object-transformer>


来源:https://stackoverflow.com/questions/20240745/cannotresolveclassexception-while-trying-to-transform-xml-string-into-java-objec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!