From: Gustavo Martin Morcuende Date: Sun, 31 May 2015 11:10:13 +0000 (+0200) Subject: Custom xs:dateTime binder X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=195741bfd226242651caea90a2fad903d1622ef1;p=SpringWebServicesForFun%2F.git Custom xs:dateTime binder --- diff --git a/web-services-spring-server/src/main/java/de/spring/webservices/binders/XSDateTimeCustomBinder.java b/web-services-spring-server/src/main/java/de/spring/webservices/binders/XSDateTimeCustomBinder.java new file mode 100644 index 0000000..46076f4 --- /dev/null +++ b/web-services-spring-server/src/main/java/de/spring/webservices/binders/XSDateTimeCustomBinder.java @@ -0,0 +1,30 @@ +package de.spring.webservices.binders; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import javax.xml.bind.annotation.adapters.XmlAdapter; + + +public class XSDateTimeCustomBinder extends XmlAdapter { + + @Override + public Date unmarshal(final String dateTime) throws Exception { + // X pattern just works from Java >= 1.7 + final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); + formatter.setTimeZone(TimeZone.getTimeZone("Europe/Madrid")); + + return formatter.parse(dateTime); + } + + @Override + public String marshal(final Date dateTime) throws Exception { + // X pattern just works from Java >= 1.7 + final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); + formatter.setTimeZone(TimeZone.getTimeZone("Europe/Madrid")); + + return formatter.format(dateTime); + } +} diff --git a/web-services-spring-server/src/main/resources/bindings/custombinding.xjb b/web-services-spring-server/src/main/resources/bindings/custombinding.xjb index eea1cfe..726784a 100644 --- a/web-services-spring-server/src/main/resources/bindings/custombinding.xjb +++ b/web-services-spring-server/src/main/resources/bindings/custombinding.xjb @@ -4,8 +4,9 @@ xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance" xmlns:annox="http://annox.dev.java.net" + xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:version="2.1" - jaxb:extensionBindingPrefixes="inheritance annox"> + jaxb:extensionBindingPrefixes="xjc inheritance annox"> + + + + + diff --git a/web-services-spring-server/src/main/resources/xsd/examples.xsd b/web-services-spring-server/src/main/resources/xsd/examples.xsd index 7f431c6..ec642a9 100644 --- a/web-services-spring-server/src/main/resources/xsd/examples.xsd +++ b/web-services-spring-server/src/main/resources/xsd/examples.xsd @@ -45,6 +45,7 @@ + @@ -52,6 +53,7 @@ +