Sorry for what it seems to be a redundant question but i do have many options, and despite my effort reading dozen of threads, i'm not able to understand what to do.
I do have a java application which job is to :
Plan is thus as follows :
SOAP WS ---- deliverydate ----> JAVA APP (logic) ---- datebl ----> REST WS
Jar used in JAVA APP are jackson-core-asl-1.9.13.jar
and jackson-mapper-asl-1.9.13.jar
among others.
Issues i have is when dealing with dates.
Readings : ( Tried to be inspired but jackson version seems not to be the same ) :
JSON Serializing date in a custom format (Can not construct instance of java.util.Date from String value)
Jackson 2.3.2: Issue with deserializing a Date despite of setting the date format to ObjectMapper
Edit 01/04/15
http://jackson-users.ning.com/forum/topics/cannot-deserialize-a-date
The facts now
Point 1 : When recovering data from SOAP WS, deliverydate is a String which exact value is :
2014-07-31 07:00:00.0
Point 2: Just before using the setter, i thought it could be a good idea to format this String to a 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 this stage, datebl value has transformed to
Thu Jul 31 07:00:00 CEST 2014
(despite choosing the specific format yyyy-MM-dd HH:mm:ss)
Point 3 and ERROR i have : The error i have is thrown by the rest server:
Can not construct instance of java.util.Date from String value 'Thu Jul 31 07:00:00 CEST 2014': not a valid representation (error: Can not parse date "Thu Jul 31 07:00:00 CEST 2014": not compatible with any 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")) at [Source: org.glassfish.jersey.message.internal.EntityInputStream@5709779; line: 1, column: 74] (through reference chain: com.rest.entities.MvtSwapsIn["datebl"])
What i tried to do : To resolve this, as i'm using a version prior to 2.x, i thought that my best option was to use a custom serializer, so :
In pojo, annotation was added just before the getter
@JsonSerialize(using = CustomJsonDateSerializer.class)
public java.util.Date getDatebl() {
return datebl;
}
Serializer was created as 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 with FORMAT_HEURE_JSON_SERIALIZE_2, but 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 this point, i'm lost.
I don't know where and how to update my code.
Please can someone help me organize my thoughts ?
Kind regards,
Pierre
Open your POJO, annotate the date field declaration like so
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "<your_date_pattern>")
Where your_date_pattern can be something like
yyyy-MM-dd HH:mm:ss
Done
I guess you are using Jersey as a JAX-RS implementation. Have you left some details out from the stacktrace? Reading the stacktrace it seems the Jersey receives a String instead of a Date: Can not construct instance of java.util.Date from String value 'Thu Jul 31 07:00:00 CEST 2014'
. If your class com.rest.entities.MvtSwapsIn["datebl"])
declares a date, this behaviour is a bit strange.
Anyway, for Jackson to work, one suggestion is to register a Jackson ObjectMapper to the REST config with a ContextResolver (this applies to both Jackson 1.x and 2.x). Try putting a breakpoint in both the constructor and getContext()-method to see if they are invoked at 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 up your configured ObjectMapper. If no you can do it manually:
@ApplicationPath("/")
public class RestApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<>();
classes.add(ObjectMapperConfigContextResolver.class);
return classes;
}
}
It would be helpful if you provided some information from your pom.xml (if you are using Maven).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With