package cn.common.page.converter;

import java.io.IOException;
import java.lang.reflect.Type;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import cn.common.page.response.Result;
import cn.util.core.LogKit;

/**见注解中 自定义转换器*/
public class ResResult2HttpMsg extends MappingJackson2HttpMessageConverter {

    public ResResult2HttpMsg() {
        LogKit.info("load {}", this.getClass().getName());
    }

    @Override
    protected void writeInternal(final Object object, final Type type, final HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {
    	if (object instanceof Result) {
            super.writeInternal(object, type, outputMessage);
        } 
        else {
            final Result<?> result = Result.ok(object);
            super.writeInternal(result, type, outputMessage);
        }
    }

    @Override
    protected void writeInternal(final Object object, final HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {
        this.writeInternal(object, null, outputMessage);
    }
}
