1

I'm migrating my application (Jee7) from Wildfly 9.0.1 to Wildfly 16.0.0.

I noticed different Responses from JAX-RS json (java.util.Date) deserialization on both wildfly version.

Is it a bug or Jee spec changed?

Is there a way to globally fix it for entire application?

Example classes:

@ApplicationPath("/rest")
public class RestConfig extends Application {

}

@Path("/test")
public class TestResource {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public TestEntity get() {
        return new TestEntity(new Date());
    }
}

public class TestEntity {

    private Date dtTest;

    /* other fields */

    public TestEntity(Date dtTest) {
        super();
        this.dtTest = dtTest;
    }

    public Date getDtTest() {
        return dtTest;
    }


}

Wildfly 9.0.1 Response: {"dtTest":1558550586974}

Wildfly 16.0.0 Response: {"dtTest":"2019-05-22T18:44:47.268Z[UTC]"}

I'd like to get 1558550586974 for "dtTest" as response from Wildfly 16.

Renato Silva
  • 108
  • 7
  • Looks like there are settings like `SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS`, as seen in https://stackoverflow.com/questions/4428109/jersey-jackson-json-date-format-serialization-how-to-change-the-format-or-us. Is that something that helps? – Progman May 24 '19 at 18:09
  • Progman, thanks for the answer. It didn't work for me. But the problem solution uses the same strategy (ContextResolver) which was found at: https://developer.jboss.org/thread/279220 – Renato Silva May 24 '19 at 22:48

1 Answers1

3

The solution found at https://developer.jboss.org/thread/279220.

I changed pom.xml dependecy from Jee7 to Jee8:

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0</version>
            <scope>provided</scope>
        </dependency>

I created a provider implementing ContextResolver

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;
import javax.json.bind.annotation.JsonbDateFormat;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JsonbDateConfig implements ContextResolver<Jsonb> {  
    private final Jsonb jsonB;  


    public JsonbDateConfig()  
    {  
        JsonbConfig config = new JsonbConfig();  
        config.setProperty(JsonbConfig.DATE_FORMAT, JsonbDateFormat.TIME_IN_MILLIS);  
        jsonB = JsonbBuilder.create(config);  
    }  


    @Override  
    public Jsonb getContext(Class objectType) {  
        return jsonB;  
    }  
}  

And that solved the problem.

Renato Silva
  • 108
  • 7