JAXB, Custom bindings, Adapter1.class and Joda-time

前端 未结 7 1610
时光说笑
时光说笑 2021-01-11 15:38

I have a problem with the way JAXB is generating the bound classes for an XML schema (which, for sake of precision, I cannot modify). I want to map a xsd:date type to a Joda

7条回答
  •  别那么骄傲
    2021-01-11 15:55

    A complete example. This is your bindings.xml:

    
        
           
              
           
        
    
    

    plus the Java Class:

    package com.yourpackage;
    
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    
    import javax.xml.bind.DatatypeConverter;
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class DateAdapter extends XmlAdapter
    {
    
        @Override
        public Date unmarshal(final String date) {
            return DatatypeConverter.parseDate(date).getTime();
        }
    
        @Override
        public String marshal(final Date date)
        {
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            return DatatypeConverter.printDate(calendar);
        }
    
    }
    

    plus the pom.xml definition:

    
        org.apache.cxf
        cxf-codegen-plugin
        ${cxf.version}
        
            
                org.jvnet.jaxb2_commons
                jaxb2-basics
                0.6.4
            
        
        
            
                generate-sources
                generate-sources
                
                    ${project.build.directory}/generated/cxf
                    
                        true
                    
                    
                        
                            ${basedir}/src/main/resources/your.wsdl
                            
                                -verbose
                                -xjc-npa
                                -xjc-Xsetters
                            
                            
                            ${basedir}/src/main/resources/binding.xml
                            
                        
                    
                
                
                    wsdl2java
                
            
        
    
    

提交回复
热议问题