Piyush
Piyush

Reputation: 1162

Error during wsimport with additional JAXB bindings

I am running wsimport from cmd with my dataBindings in bindings.xml file. Basically I am trying to override the default binding for "datetime" schema type to generate "java.util.Date" instead of "XMLGregorianCalendar".

Here is the command I use.

wsimport -keep -verbose -extension -b bindings.xml -wsdllocation http://localhost/wsdl/API http://localhost:8080/cc/API?WSDL -d C:/Users/ab/Desktop/test

My Bindings.xml looks like this.

<bindings xmlns="http://java.sun.com/xml/ns/jaxb" version="2.1" >
    <globalBindings>
        <javaType name="java.util.Date" xmlType="xs:dateTime"
        parseMethod="adapter.DateAdapter.marshal"
        printMethod="adapter.DateAdapter.unmarshal" />
    </globalBindings>
</bindings>

Here is my DateAdapter class:

package adapter;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.bind.DatatypeConverter;

public class DateAdapter
{
    public static Date unmarshal(String paramString)
    {
        return DatatypeConverter.parseDate(paramString).getTime();
    }

    public static String marshal(Date paramDate)
    {
        Calendar localCalendar = GregorianCalendar.getInstance();
        localCalendar.setTime(paramDate);
        return DatatypeConverter.printDateTime(localCalendar);
    }
}

Running wsimport generates the webService files along with a Adapter1.class. However, the response contains the following error.

C:\Users\ab\Desktop\test\org\w3\_2001\xmlschema\Adapter1.java:13: package a
dapter does not exist
        return (adapter.DateAdapter.marshal(value));

Now, my DateAdapter.class is located in the folder "adapter" at same location I am running wsimport from.

I want to know why is it not able to read the DateAdapter? I also have tried keeping the file at the root instead of in folder but that too throws similar exception.

Upvotes: 0

Views: 1181

Answers (1)

Piyush
Piyush

Reputation: 1162

Just an update on how I fixed the problem.

I found two ways to make the date conversion work.

The wsimport command used in both cases is mentioned in the question. Also, I used jdk1.6.0_45 in both cases.

1. Not using a custom DateAdapter.class

In this case, I directly used the javax.xml.bind.DatatypeConverter for the datetime schema-type. Here is the binding.

<jaxb:globalBindings>
    <jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime" parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
     printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
   </jaxb:globalBindings>
</jaxb:bindings>

2. Using a custom DateAdapter.class

I figured that using the DateAdapter.java without a package statement makes JAXB not able to put an import for the DateAdapter in the Adapter1.class that it generates. So, I put the DateAdapter.java inside a package(com.test.date) and referenced it inside the bindings with the complete path like below.

<jaxb:globalBindings>
    <jaxb:javaType name="java.util.Date" xmlType="xs:dateTime" parseMethod="com.test.date.DateAdapter.unmarshal" 
        printMethod="com.test.date.DateAdapter.marshal"> </jaxb:javaType>
   </jaxb:globalBindings>
</jaxb:bindings>

The DateAdapter.java looks like this.

package com.test.date;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.bind.DatatypeConverter;

public class DateAdapter {
    private DateAdapter() {
    }

    public static String marshal(Date date) {
        System.out.println("Inside marshal");
        Calendar cal = GregorianCalendar.getInstance();
        cal.setTime(date);
        return DatatypeConverter.printDateTime(cal);
    }

    public static Date unmarshal(String xmlDate) {
        System.out.println("Inside unmarshal");
        return DatatypeConverter.parseDate(xmlDate).getTime();
    }

}

Now, this does throw a compilation error but it correctly generates the Adapter1.java (with the correct DateApapter import).

Upvotes: 1

Related Questions