java - How to use @XmlJavaTypeAdapter in @RestController parameters? -
i have following restcontroller, , get-query controller thedate=2016-08-08
format.
it should automatically converted java.time.localdate
. xmladapter
not working. why?
import java.time.localdate; import java.time.format.datetimeformatter; import javax.xml.bind.annotation.adapters.xmladapter; public class localdateadapter extends xmladapter<string, localdate> { @override public localdate unmarshal(string v) throws exception { return localdate.parse(v, datetimeformatter.iso_local_date); } @override public string marshal(localdate v) throws exception { return datetimeformatter.iso_local_date.format(date); } } @restcontroller public class myservlet { @requestmapping(value = "/", method = requestmethod.get) private string test(restparams p) { } } @xmlrootelement @xmlaccessortype(xmlaccesstype.field) public class restparams { @valid @notnull @xmlelement(required = true, nillable = false) @xmljavatypeadapter(value = localdateadapter.class) private localdate thedate; }
result:
'thedate': failed convert property value of type [java.lang.string] required type [java.time.localdate] property 'thedate'; nested exception org.springframework.core.convert.conversionfailedexception: failed convert type [java.lang.string] type [@javax.validation.valid @javax.validation.constraints.notnull @javax.xml.bind.annotation.xmlelement @javax.xml.bind.annotation.adapters.xmljavatypeadapter java.time.localdate] value '2016-08-08'; nested exception java.time.format.datetimeparseexception: text '2016-08-08' not parsed @ index 2
i don't know why it's not working, solved using spring annotation:
@org.springframework.format.annotation.datetimeformat.datetimeformat(iso = iso.date) private java.time.localdate thedate;
Comments
Post a Comment