com.uimirror.core.framework.rest.util.ObjectMapperFactory.java Source code

Java tutorial

Introduction

Here is the source code for com.uimirror.core.framework.rest.util.ObjectMapperFactory.java

Source

/*******************************************************************************
 * Copyright (c) 2015 Uimirror.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Uimirror license
 * which accompanies this distribution, and is available at
 * http://www.uimirror.com/legal
 * <p>
 * Contributors: Jay
 * Uimirror Team
 *******************************************************************************/

package com.uimirror.core.framework.rest.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;

import static java.lang.Boolean.FALSE;

/**
 * {@link ObjectMapperFactory}
 * @author Jay
 **/
public class ObjectMapperFactory {

    public static ObjectMapperFactory factory;

    private ObjectMapperFactory() {
        //NOP
    }

    public static ObjectMapper getDefaultMapper() {
        return new ObjectMapper().configure(DeserializationFeature.UNWRAP_ROOT_VALUE, FALSE)
                .configure(SerializationFeature.WRAP_ROOT_VALUE, FALSE).enable(SerializationFeature.INDENT_OUTPUT)
                .setSerializationInclusion(JsonInclude.Include.NON_NULL)
                .setAnnotationIntrospector(getAnnotationPair());

    }

    private static AnnotationIntrospector getAnnotationPair() {
        final AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
        final AnnotationIntrospector secondary = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
        return AnnotationIntrospector.pair(primary, secondary);
    }

}