All Downloads are FREE. Search and download functionalities are using the official Maven repository.

JavaJaxRS.resteasy.JacksonConfig.mustache Maven / Gradle / Ivy

There is a newer version: 3.0.0-rc1
Show newest version
package {{invokerPackage}};

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
{{#java8}}
    import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
{{/java8}}
{{#joda}}
    import com.fasterxml.jackson.datatype.joda.JodaModule;
    import org.joda.time.DateTime;
    import org.joda.time.LocalDate;
    import org.joda.time.format.ISODateTimeFormat;
{{/joda}}

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import java.io.IOException;

@Provider
public class JacksonConfig implements ContextResolver {
    private final ObjectMapper objectMapper;

    public JacksonConfig() throws Exception {

        objectMapper = new ObjectMapper()
            .setDateFormat(new RFC3339DateFormat()){{#legacyDates}};{{/legacyDates}}
{{#java8}}
            .registerModule(new JavaTimeModule());
{{/java8}}
{{#joda}}
            .registerModule(new JodaModule() {
                {
                    addSerializer(DateTime.class, new StdSerializer(DateTime.class) {
                        @Override
                        public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
                            jgen.writeString(ISODateTimeFormat.dateTimeNoMillis().print(value));
                        }
                    });
                    addSerializer(LocalDate.class, new StdSerializer(LocalDate.class) {
                        @Override
                        public void serialize(LocalDate value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
                            jgen.writeString(ISODateTimeFormat.date().print(value));
                        }
                    });

                }
            });
{{/joda}}
    }

    public ObjectMapper getContext(Class arg0) {
        return objectMapper;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy