开发者

Mapping Java Date Object to XML Schema datetime format

I am having some problem mapping my Java Data Type to standard Schema Date data type.

I have a simple class that I annotated like this. The period instance variable is of Java Date object type.

@XmlAccessorType(value = XmlAccessType.NONE)
public class Chart {
    @XmlElement
    private double amount;
    @XmlElement
    private double amountDue;
    @XmlElement
    private Date period;
    //constructor getters and setters
}

Here is my Web Service

@WebService
public class ChartFacade {
    @WebMethod
    public Chart getChart() throws ParseException {
      SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
      Chart chart = new Chart(20.0,20.5, df.parse("2001-01-01"));
      return chart;
    }
}

My problem is it returns the date data in a format not according to what I am expecting.

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:getChartResponse xmlns:ns2="http://ss.ugbu.oracle.com/">
         <return>
            <amount>20.0</amount>
            <amountDue>20.5</amountDue>
            **<period>2001-01-01T00:01:00+08:00</period>**
         </return>
      </ns2:getChartResponse>
   </S:Body>
</S:Envelope>

I wanted the period element to be returned like this

<period>2001-01-01</period>

Is there any way I can ac开发者_Python百科hieve this?


You can do the following to control the schema type:

@XmlElement
@XmlSchemaType(name="date")
private Date period;

For More Information:

  • http://bdoughan.blogspot.com/2011/01/jaxb-and-datetime-properties.html


Use @XmlJavaTypeAdapter annotation and you can marshal/unmarshal your fields any way you want.

Cannot tell though if it's the simplest way.

And note also that it may harm interoperability with any code that would try to use your WSDL. The programmers for that other code would see xsd:string as the field type, and therefore will have to do formatting and parsing manually (just like you do, yes), introducing who knows how many bugs. So please consider if the xsd:date a bad choice really.

Stolen from here:

@XmlJavaTypeAdapter(value=DateAdapter.class, type=Date.class)
Date someDate;
...

public class DateAdapter extends XmlAdapter<String, Date> {

    // the desired format
    private String pattern = "MM/dd/yyyy";

    public String marshal(Date date) throws Exception {
        return new SimpleDateFormat(pattern).format(date);
    }

    public Date unmarshal(String dateString) throws Exception {
        return new SimpleDateFormat(pattern).parse(dateString);
    }   
}

UPDATE: as was mentioned by @Blaise Doughan, a much shorter way is to annotate the date with

@XmlSchemaType("date")
Date someDate;

Despite it is still not clear why timezone information is not generated for the date, this code works in practice and requires much less typing.


Your Chart constructor seems to be parsing the formatted date string back into a Date, which is then being serialized using the default format to the XML response. I guess using private String period; (and fixing the constructors) should work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜