Getting com.thoughtworks.xstream.mapper.CannotResolveClassException while trying to unmarshal using XStream

StackOverflow https://stackoverflow.com/questions/21699627

  •  09-10-2022
  •  | 
  •  

Question

I am getting below exception while trying to unmarshal in XStream.

2014-02-11 16:08:00,022 ERROR  - com.healthways.checkservices.codes.XMLTransformer : name : name : name : name
---- Debugging information ----
message             : name : name
cause-exception     : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : name : name
class               : com.healthways.checkservices.beans.ServiceDetailsList
required-type       : com.healthways.checkservices.beans.ServiceDetailsList
path                : /allServices/name
-------------------------------
2014-02-11 16:08:00,022 ERROR [Ljava.lang.StackTraceElement;@68d91916
2014-02-11 16:08:00,023 ERROR  - com.healthways.checkservices.codes.XMLTransformer : com.thoughtworks.xstream.mapper.CannotResolveClassException: name : name**

Actually it worked fine until today. Suddenly I started getting this error ever since I added few plugins to mu=y Eclipse Kepler IDE. I am fulfilling all possible prerequisites before passing the xml. My code snippet is mentioned below-

XStream xstream = new XStream(new DomDriver());

         xstream.alias("service", ServiceDetails.class);
         xstream.alias("allServices", ServiceDetailsList.class);

         xstream.addImplicitCollection(ServiceDetailsList.class, "list");

         // Serialize the XML to Java object

            try {
                logger.info( "XML Path : " + CommonConstants.FOLDER_PATH);
                File inputXMLFile = new File(CommonConstants.FOLDER_PATH+CommonConstants.INPUT_XML_NAME);

                if (inputXMLFile.exists()){
                    fis = new FileInputStream(inputXMLFile);

                    //De-serializing XML to Java object
                    ServiceDetailsList serviceList = new ServiceDetailsList();
                    try {
                        serviceList = (ServiceDetailsList)xstream.fromXML(fis);
                    }catch (CannotResolveClassException crce) {
                        // TODO Auto-generated catch block
                        logger.error(crce.getMessage());
                    }catch (ConversionException ce) {
                        // TODO Auto-generated catch block
                        logger.error(ce.getMessage());
                        logger.error(ce.getStackTrace());
                        logger.error(ce.getCause());
                    }

Supporting files------

ServiceDetails.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- edited with XMLSpy v2009 (http://www.altova.com) by kevin m forbes (Healthways) -->
<allServices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="services.xsd">
    <service>
        <name>FlipKartSvc</name>
        <type>WCF</type>
        <serverName>corpngdevapp01</serverName>
        <serverPort>7080</serverPort>
        <env>DEV</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.flipkart.com/</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    </service>
    <name>FlipKartSvc</name>
        <type>WCF</type>
        <serverName>corpngdevapp03</serverName>
        <serverPort>7080</serverPort>
        <env>DEV</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.flipkart.com/</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    <service>
        <name>GoogleSvc</name>
        <type>MB</type>
        <serverName>corpngqaapp02</serverName>
        <serverPort>7080</serverPort>
        <env>QA</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.google.co.in</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    </service>
    <service>
        <name>GoogleSvc</name>
        <type>MB</type>
        <serverName>corpnguatapp01</serverName>
        <serverPort>7080</serverPort>
        <env>QA</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.google.co.in</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    </service>
    <service>
        <name>GoogleSvc</name>
        <type>MB</type>
        <serverName>corpnguatapp01</serverName>
        <serverPort>7080</serverPort>
        <env>QA</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.google.co.in</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    </service>
    <service>
        <name>FlipKartSvc</name>
        <type>WCF</type>
        <serverName>corpnguatapp01</serverName>
        <serverPort>7080</serverPort>
        <env>DEV</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.flipkart.com/</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    </service>
    <service>
        <name>FlipKartSvc</name>
        <type>WCF</type>
        <serverName>corpnguatapp02</serverName>
        <serverPort>7080</serverPort>
        <env>DEV</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.flipkart.com/</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    </service>
    <service>
        <name>GoogleSvc</name>
        <type>MB</type>
        <serverName>corpngqaapp01</serverName>
        <serverPort>7080</serverPort>
        <env>QA</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.google.co.in</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    </service>
    <service>
        <name>FlipKartSvc</name>
        <type>WCF</type>
        <serverName>corpngdevapp02</serverName>
        <serverPort>7080</serverPort>
        <env>DEV</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.flipkart.com/</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    </service>
</allServices>

ServiceDetails.java


public class ServiceDetails {

    private String name;
    private String type;
    private String serverName;
    private String serverPort;
    private String env;
    private String appsUsed;
    private String url;
    private String emailIds;
    private String statusCode;
    private String statusMsg;
    private boolean sendMail;

    /**
     * @return the sendMail
     */
    public boolean isSendMail() {
        return sendMail;
    }
    /**
     * @param sendMail the sendMail to set
     */
    public void setSendMail(boolean sendMail) {
        this.sendMail = sendMail;
    }
    /**
     * @return the env
     */
    public String getEnv() {
        return env;
    }
    /**
     * @param env the env to set
     */
    public void setEnv(String env) {
        this.env = env;
    }
    /**
     * @return the appsUsed
     */
    public String getAppsUsed() {
        return appsUsed;
    }
    /**
     * @param appsUsed the appsUsed to set
     */
    public void setAppsUsed(String appsUsed) {
        this.appsUsed = appsUsed;
    }
    /**
     * @return the emailIds
     */
    public String getEmailIds() {
        return emailIds;
    }
    /**
     * @param emailIds the emailIds to set
     */
    public void setEmailIds(String emailIds) {
        this.emailIds = emailIds;
    }
    /**
     * @return the statusCode
     */
    public String getStatusCode() {
        return statusCode;
    }
    /**
     * @param statusCode the statusCode to set
     */
    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }
    /**
     * @return the statusMsg
     */
    public String getStatusMsg() {
        return statusMsg;
    }
    /**
     * @param statusMsg the statusMsg to set
     */
    public void setStatusMsg(String statusMsg) {
        this.statusMsg = statusMsg;
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the type
     */
    public String getType() {
        return type;
    }
    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }
    /**
     * @return the serverName
     */
    public String getServerName() {
        return serverName;
    }
    /**
     * @param serverName the serverName to set
     */
    public void setServerName(String serverName) {
        this.serverName = serverName;
    }
    /**
     * @return the serverPort
     */
    public String getServerPort() {
        return serverPort;
    }
    /**
     * @param serverPort the serverPort to set
     */
    public void setServerPort(String serverPort) {
        this.serverPort = serverPort;
    }
    /**
     * @return the url
     */
    public String getUrl() {
        return url;
    }
    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }

}

ServiceDetailsList.java

public class ServiceDetailsList {

     private List<ServiceDetails> list;

        public ServiceDetailsList(){
            list = new ArrayList<ServiceDetails>();
        }

        public void add(ServiceDetails sd){
            list.add(sd);
        }

        public List<ServiceDetails> getList(){
            return list;
        }
}

I would appreciate any expert suggestion on this issue. Thanks in advance.

Was it helpful?

Solution

After looking at your "ServiceDetails.xml" . I noticed that it is not a valid one... There is no <service> start and end tag surrounded by second <name>....<sendMail> tags.. So,that is giving the Exception.. First Check it .. After Correcting it that is not giving the Exception... Use the following xml instead of above one

<?xml version="1.0" encoding="utf-8"?>
<!-- edited with XMLSpy v2009 (http://www.altova.com) by kevin m forbes (Healthways) -->
<allServices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="services.xsd">
    <service>
        <name>FlipKartSvc</name>
        <type>WCF</type>
        <serverName>corpngdevapp01</serverName>
        <serverPort>7080</serverPort>
        <env>DEV</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.flipkart.com/</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    </service>
   <service>
    <name>FlipKartSvc</name>
        <type>WCF</type>
        <serverName>corpngdevapp03</serverName>
        <serverPort>7080</serverPort>
        <env>DEV</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.flipkart.com/</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    </service>       
    <service>
        <name>GoogleSvc</name>
        <type>MB</type>
        <serverName>corpngqaapp02</serverName>
        <serverPort>7080</serverPort>
        <env>QA</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.google.co.in</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    </service>
    <service>
        <name>GoogleSvc</name>
        <type>MB</type>
        <serverName>corpnguatapp01</serverName>
        <serverPort>7080</serverPort>
        <env>QA</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.google.co.in</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    </service>
    <service>
        <name>GoogleSvc</name>
        <type>MB</type>
        <serverName>corpnguatapp01</serverName>
        <serverPort>7080</serverPort>
        <env>QA</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.google.co.in</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    </service>
    <service>
        <name>FlipKartSvc</name>
        <type>WCF</type>
        <serverName>corpnguatapp01</serverName>
        <serverPort>7080</serverPort>
        <env>DEV</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.flipkart.com/</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    </service>
    <service>
        <name>FlipKartSvc</name>
        <type>WCF</type>
        <serverName>corpnguatapp02</serverName>
        <serverPort>7080</serverPort>
        <env>DEV</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.flipkart.com/</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    </service>
    <service>
        <name>GoogleSvc</name>
        <type>MB</type>
        <serverName>corpngqaapp01</serverName>
        <serverPort>7080</serverPort>
        <env>QA</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.google.co.in</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    </service>
    <service>
        <name>FlipKartSvc</name>
        <type>WCF</type>
        <serverName>corpngdevapp02</serverName>
        <serverPort>7080</serverPort>
        <env>DEV</env>
        <appsUsed>CIS</appsUsed>
        <url>http://www.flipkart.com/</url>
        <emailIds>ashish.kashyap12@gmail.com</emailIds>
        <statusCode/>
        <statusMsg/>
        <sendMail>true</sendMail>
    </service>
</allServices>

let me know if you face any issues

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top