Can not construct instance of java.util.Date from String value - not a valid representation -
sorry seems redundant question have many options, , despite effort reading dozen of threads, i'm not able understand do.
i have java application job :
- get datas soap ws (xml),
- do logic ( incoming deliverydate become datebl )
- finally send rest ws (json format), update oracle table
plan follows :
soap ws ---- deliverydate ----> java app (logic) ---- datebl ----> rest ws
jar used in java app jackson-core-asl-1.9.13.jar
, jackson-mapper-asl-1.9.13.jar
among others.
issues have when dealing dates.
readings : ( tried inspired jackson version seems not same ) :
json serializing date in custom format (can not construct instance of java.util.date string value)
jackson 2.3.2: issue deserializing date despite of setting date format objectmapper
edit 01/04/15
http://jackson-users.ning.com/forum/topics/cannot-deserialize-a-date
the facts now
point 1 : when recovering data soap ws, deliverydate string exact value :
2014-07-31 07:00:00.0
point 2: before using setter, thought idea format string date.
simpledateformat dateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss"); date dateebl = dateformat.parse(deliverydate); msi.setdatebl(dateebl);
datebl declaration in pojo
private java.util.date datebl;
at stage, datebl value has transformed
thu jul 31 07:00:00 cest 2014
(despite choosing specific format yyyy-mm-dd hh:mm:ss)
point 3 , error have : error have thrown rest server:
can not construct instance of java.util.date string value 'thu jul 31 07:00:00 cest 2014': not valid representation (error: can not parse date "thu jul 31 07:00:00 cest 2014": not compatible of standard forms ("yyyy-mm-dd't'hh:mm:ss.sssz", "yyyy-mm-dd't'hh:mm:ss.sss'z'", "eee, dd mmm yyyy hh:mm:ss zzz", "yyyy-mm-dd")) @ [source: org.glassfish.jersey.message.internal.entityinputstream@5709779; line: 1, column: 74] (through reference chain: com.rest.entities.mvtswapsin["datebl"])
what tried : resolve this, i'm using version prior 2.x, thought best option use custom serializer, :
in pojo, annotation added before getter
@jsonserialize(using = customjsondateserializer.class) public java.util.date getdatebl() { return datebl; }
serializer created follows
public class customjsondateserializer extends jsonserializer<date> { @override public void serialize(date value, jsongenerator jgen, serializerprovider provider) throws ioexception, jsonprocessingexception { simpledateformat dateformat = new simpledateformat(properties.general.format_heure_json_serialize_2); string datestring = dateformat.format(value); jgen.writestring(datestring); }
}
in example,tried format_heure_json_serialize_2, tried many others without success.
public static final string format_heure_json = new string("yyyy-mm-dd't'hh:mm:ss.sssz"); public static final string format_heure_json_serialize = new string("eee yyyy-mm-dd't'hh:mm:ss.sssz"); public static final string format_heure_json_serialize_2 = new string("eee, dd mmm yyyy hh:mm:ss zzz"); public static final string format_heure_json_serialize_3 = new string("yyyy-mm-dd hh:mm:ss");
at point, i'm lost.
i don't know , how update code.
- should still use simpledateformat after getting date soap ws ?
- given fact i'm using jackson 1.9, @jsonserialize annotation ? ( serializer ?)
- do have modify on rest server ?
please can me organize thoughts ?
kind regards,
pierre
i guess using jersey jax-rs implementation. have left details out stacktrace? reading stacktrace seems jersey receives string instead of date: can not construct instance of java.util.date string value 'thu jul 31 07:00:00 cest 2014'
. if class com.rest.entities.mvtswapsin["datebl"])
declares date, behaviour bit strange.
anyway, jackson work, 1 suggestion register jackson objectmapper rest config contextresolver (this applies both jackson 1.x , 2.x). try putting breakpoint in both constructor , getcontext()-method see if invoked @ runtime:
public class objectmapperconfigcontextresolver implements contextresolver<objectmapper> { objectmapper mapper; public objectmapperconfigcontextresolver() { mapper.setdateformat(new simpledateformat("<your pattern>")); } @override public objectmapper getcontext(class<?> type) { return mapper; }
}
the @provider annotation should make jax-rs pick configured objectmapper. if no can manually:
@applicationpath("/") public class restapplication extends application { @override public set<class<?>> getclasses() { set<class<?>> classes = new hashset<>(); classes.add(objectmapperconfigcontextresolver.class); return classes; } }
it helpful if provided information pom.xml (if using maven).
Comments
Post a Comment