Jersey + Jackson JSON date format serialization - how to change the format or use custom JacksonJsonProvider

前端 未结 8 1123
花落未央
花落未央 2020-11-30 00:54

I am using Jersey + Jackson to provide REST JSON services layer for my application. The problem I have is that the default Date serialization format looks like that:

8条回答
  •  迷失自我
    2020-11-30 01:01

    I managed to do it in Resteasy "the JAX-RS way", so it should work on every compliant implementation like Jersey (recently successfully tested on JEE7 server Wildfly 8, it just required a few changes to the Jackson part because they changed a few APIs).

    You must define a ContextResolver (check that Produces contains the correct content-type):

    import javax.ws.rs.ext.ContextResolver;
    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.map.SerializationConfig;
    import org.codehaus.jackson.map.DeserializationConfig;
    import javax.ws.rs.ext.Provider;
    import javax.ws.rs.Produces; 
    import java.text.SimpleDateFormat;
    @Provider
    @Produces("application/json")
    public class JacksonConfigurator implements ContextResolver {
    
        private ObjectMapper mapper = new ObjectMapper();
    
        public JacksonConfigurator() {
            SerializationConfig serConfig = mapper.getSerializationConfig();
            serConfig.setDateFormat(new SimpleDateFormat());
            DeserializationConfig deserializationConfig = mapper.getDeserializationConfig();
            deserializationConfig.setDateFormat(new SimpleDateFormat());
            mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
        }
    
        @Override
        public ObjectMapper getContext(Class arg0) {
            return mapper;
        }
    
    }
    

    Then you must return the newly created class in your javax.ws.rs.core.Application's getClasses

    import javax.ws.rs.core.Application;
    public class RestApplication extends Application {
    
         @Override
         public Set> getClasses() {
             Set> classes = new HashSet>();
             // your classes here
             classes.add(JacksonConfigurator.class);
             return classes;
          }
    
    }
    

    this way all operation made through jackson are given the ObjectMapper of your choice.

    EDIT: I recently found out at my expenses that using RestEasy 2.0.1 (and thus Jackson 1.5.3) there is a strange behaviour if you decide to extend the JacksonConfigurator to add custom mappings.

    import javax.ws.rs.core.MediaType;
    @Provider
    @Produces(MediaType.APPLICATION_JSON)
    public class MyJacksonConfigurator extends JacksonConfigurator
    

    If you just do like this (and of course put the extended class in RestApplication) the mapper of the parent class is used, that is you lose the custom mappings. To make it correctly work I had to do something that seems useless to me otherwise:

    public class MyJacksonConfigurator extends JacksonConfigurator implements ContextResolver 
    

提交回复
热议问题