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

org.xlcloud.rest.client.config.ObjectMapperProvider Maven / Gradle / Ivy

Go to download

This module provides base classes for creation of REST client (SDK). Default client has preconfigured filters, Oauth2 token injection or even object mapper.

The newest version!
/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.xlcloud.rest.client.config;

import javax.enterprise.inject.Produces;
import javax.ws.rs.ext.ContextResolver;

import org.xlcloud.rest.serialization.JSONObjectSerializer;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.AnnotationIntrospector.Pair;
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.module.SimpleModule;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;

/**
 * This class produces {@link ObjectMapper} used for serialization of entities
 * exchanged with the REST server.
 * 
 * @author Tomek Adamczewski, AMG.net
 * @author Piotr Kulasek-Szwed, AMG.net
 */
public final class ObjectMapperProvider implements ContextResolver {

    private final ObjectMapper objectMapper;

    public ObjectMapperProvider() {
        objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
        objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
        objectMapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
        objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
        // objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL,
        // true);

        objectMapper.setSerializationInclusion(Include.NON_EMPTY);

        objectMapper.setAnnotationIntrospector(new Pair(new JacksonAnnotationIntrospector(), new JaxbAnnotationIntrospector()));
        
        SimpleModule xlcloudExtensions = new SimpleModule("XLcloudJacksonModule", Version.unknownVersion());
        xlcloudExtensions.addSerializer(new JSONObjectSerializer());
        
        objectMapper.registerModule(xlcloudExtensions);
    }

    @Override
    public ObjectMapper getContext(Class type) {
        JsonRootName jsonRootName = type.getAnnotation(JsonRootName.class);
        objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, jsonRootName != null);
        objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, jsonRootName != null);
        return objectMapper;
    }
    
    @Produces
    ObjectMapper procuceMapper(){
        return objectMapper;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy