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

com.feingto.cloud.config.feign.JacksonEncoder Maven / Gradle / Ivy

package com.feingto.cloud.config.feign;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.feingto.cloud.core.json.JSONObjectMapper;
import feign.RequestTemplate;
import feign.codec.EncodeException;
import feign.codec.Encoder;

import java.lang.reflect.Type;

/**
 * Jackson 序列化
 *
 * @author longfei
 */
public class JacksonEncoder implements Encoder {
    private final static JSONObjectMapper mapper = new JSONObjectMapper();

    @Override
    public void encode(Object object, Type bodyType, RequestTemplate template) {
        try {
            JavaType javaType = mapper.getTypeFactory().constructType(bodyType);
            template.body(mapper.writerFor(javaType).writeValueAsString(object));
        } catch (JsonProcessingException e) {
            throw new EncodeException(e.getMessage(), e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy