How to set date and time bindings using JAXWS and APT?

Im uses JAXWS 2.1.7, using some classes to run through JAXWS 'apt' to generate WSDL. For dates I use

@XmlSchemaType(name="time")  
private Date wakeupTime;

and this generates a schema with xs: time, but when it all comes out in XML, this value is like

<wakeupTime>1901-01-01T01:00:00 +10</wakeupTime>

I want ONLY time to arrive! I think I want to use my own converter to say that xs: time + java.util.Date should be printed and parsed in this way, but I cannot see that I can pass the binding file to the apt procedure. I can not (for historical and other reasons) use XMLGregorianCalendar- it should be java.util.Date. How to specify custom binding for apt tool in jaxb

+3
source share
1 answer

Ok, found it! Read this link: http://weblogs.java.net/blog/2005/04/22/xmladapter-jaxb-ri-ea
and use javax.xml.bind.annotation.adapters.XmlAdapter. those.

public class TimeFromDateAdapter extends XmlAdapter<XMLGregorianCalendar, Date>
{
  public Date unmarshal(XMLGregorianCalendar value)
  {
    Calendar cal = value.toGregorianCalendar();
    Date d = cal.getTime();
    return d;
  }

  public XMLGregorianCalendar marshal(Date value)
  {
    Calendar cal = Calendar.getInstance();
    cal.setTime(value);
    try
    {
    XMLGregorianCalendar xmlcal =   DatatypeFactory.newInstance().newXMLGregorianCalendarTime(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND), 0);
    return xmlcal;
    }
    catch (Exception e)
    {
      e.printStackTrace();
      return null;
    }
  }
}

and then:
@XmlSchemaType (name = "Time")
@XmlJavaTypeAdapter (mypackage.TimeFromDateAdapter.class)
private Date wakeupTime;

and go away.

+8
source

Source: https://habr.com/ru/post/1747823/


All Articles