I am having some problem mapping my Java Data Type to standard Schema Date data type.
我在将Java数据类型映射到标准模式日期数据类型时遇到了一些问题。
I have a simple class that I annotated like this. The period instance variable is of Java Date object type.
我有一个简单的类,我这样注释。周期实例变量是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
}
Here is my Web Service
这是我的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;
}
}
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 achieve this?
有什么办法我能做到吗?
3 个解决方案
#1
8
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
- http://bdoughan.blogspot.com/2011/01/jaxb-and-datetime-properties.html
#2
7
Use @XmlJavaTypeAdapter annotation and you can marshal/unmarshal your fields any way you want.
使用@XmlJavaTypeAdapter注释,您可以以任何您想要的方式对字段进行编组/取消编组。
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.
还要注意的是,它可能会损害尝试使用您的WSDL的任何代码的互操作性。其他代码的程序员将会看到xsd:string作为字段类型,因此必须手工进行格式化和解析(就像您一样,是的),介绍谁知道有多少错误。因此,请考虑一下xsd:约会是否真的是个糟糕的选择。
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
更新:正如@Blaise Doughan提到的,一种更短的方式是注释日期
@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.
尽管目前还不清楚为什么不为日期生成时区信息,但这一代码在实践中工作,需要更少的输入。
#3
0
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
您的图表构造函数似乎正在将格式化的日期字符串解析为日期,然后使用XML响应的默认格式对日期进行序列化。我猜是使用私有字符串周期;(以及安装施工人员)应该可以工作
#1
8
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
- http://bdoughan.blogspot.com/2011/01/jaxb-and-datetime-properties.html
#2
7
Use @XmlJavaTypeAdapter annotation and you can marshal/unmarshal your fields any way you want.
使用@XmlJavaTypeAdapter注释,您可以以任何您想要的方式对字段进行编组/取消编组。
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.
还要注意的是,它可能会损害尝试使用您的WSDL的任何代码的互操作性。其他代码的程序员将会看到xsd:string作为字段类型,因此必须手工进行格式化和解析(就像您一样,是的),介绍谁知道有多少错误。因此,请考虑一下xsd:约会是否真的是个糟糕的选择。
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
更新:正如@Blaise Doughan提到的,一种更短的方式是注释日期
@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.
尽管目前还不清楚为什么不为日期生成时区信息,但这一代码在实践中工作,需要更少的输入。
#3
0
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
您的图表构造函数似乎正在将格式化的日期字符串解析为日期,然后使用XML响应的默认格式对日期进行序列化。我猜是使用私有字符串周期;(以及安装施工人员)应该可以工作