simonC
simonC

Reputation: 4337

how to poperly deserialize dates in resteasy 3

I Use resteasy 3.0.8.Final in wildfly8.1. When I POST some date in my json I get

Can not construct instance of java.util.Date from String value '2014-06-20 12:00:00': not a valid representation (error: Failed to parse Date value '2014-06-20 12:00:00': Can not parse date "2014-06-20 12:00:00": 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"))

I had tried also with the standard formats mentioned but I get the same message.

How can I configure resteasy to parse the date from a format I want?

Upvotes: 2

Views: 3642

Answers (2)

JamesR
JamesR

Reputation: 101

You will need to include the Jackson annotations package used by rest easy.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.3.2</version>
    <scope>provided</scope>
</dependency>

Next you will need to annotate your Date fields with the JsonFormat annotation to specify the output format.

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")

The other option is to post your date's in one of the supported formats.

Upvotes: 2

lefloh
lefloh

Reputation: 10961

You can extend the JacksonJsonProvider:

@Provider
public class JsonProvider extends JacksonJsonProvider {

    public JsonProvider() {
        ObjectMapper objectMapper = locateMapper(ObjectMapper.class, MediaType.APPLICATION_JSON_TYPE);
        objectMapper.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));
        objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
    }

}

You need to add the resteasy-jackson-provider to your classpath:

<dependency>
  <groupId>org.jboss.resteasy</groupId>
  <artifactId>resteasy-jackson-provider</artifactId>
  <version>3.0.8.Final</version>
</dependency>

The SerializationConfig.Feature has changed name and package a view times. You can find it in org.codehaus.jackson.map.SerializationConfig.

Upvotes: 0

Related Questions