Mar*_*ada 7 java xml xsd web-services jaxb
我在将Java数据类型映射到标准Schema Date数据类型时遇到了一些问题.
我有一个简单的类,我这样注释.period实例变量是Java Date对象类型.
@XmlAccessorType(value = XmlAccessType.NONE)
public class Chart {
@XmlElement
private double amount;
@XmlElement
private double amountDue;
@XmlElement
private Date period;
//constructor getters and setters
}
Run Code Online (Sandbox Code Playgroud)
这是我的Web服务
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是它以不符合我期望的格式返回日期数据.
<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>
Run Code Online (Sandbox Code Playgroud)
我希望像这样返回句点元素
<period>2001-01-01</period>
Run Code Online (Sandbox Code Playgroud)
有什么办法可以实现吗?
您可以执行以下操作来控制架构类型:
@XmlElement
@XmlSchemaType(name="date")
private Date period;
Run Code Online (Sandbox Code Playgroud)
欲获得更多信息:
使用@XmlJavaTypeAdapter注释,您可以以任何方式编组/解组您的字段.
不知道它是否是最简单的方法.
另请注意,它可能会损害与尝试使用您的WSDL的任何代码的互操作性.其他代码的程序员会将xsd:string视为字段类型,因此必须手动进行格式化和解析(就像你一样,是的),介绍谁知道有多少错误.所以请考虑一下xsd:date是不是一个糟糕的选择.
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
更新:正如@Blaise Doughan所提到的,更简短的方法是用日期注释日期
@XmlSchemaType("date")
Date someDate;
Run Code Online (Sandbox Code Playgroud)
尽管仍然不清楚为什么没有为该日期生成时区信息,但此代码在实践中起作用并且需要更少的输入.
| 归档时间: |
|
| 查看次数: |
22274 次 |
| 最近记录: |