码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Message Converter

时间:2017-12-21 14:39:28      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:springconv   spring converter   

Spring Message Converter


疑问点:NamingStrategy是对于json的name修改,还是javabean的name修改???

其他的MessageConterver


MappingJackson2HttpMessageConverter:

XML

        <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
         <property name="objectMapper">  
             <bean class="com.lxzl.util.CmsObjectMapper">  
                 <!-- 处理responseBody 里面日期类型 -->  
                 <property name="dateFormat">  
                       <bean class="java.text.SimpleDateFormat">  
                           <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />  
                       </bean>  
                  </property>
                 <!-- 为null字段时不显示 -->  
                 <property name="serializationInclusion">  
                     <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>  
                 </property>  
                 <property name="propertyNamingStrategy">  
                     <bean class="com.lxzl.util.CmsPropertyNamingStrategy" />  
					<!-- <bean class="com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy" />   -->
                 </property>  
             </bean>  
         </property>  
         <property name="supportedMediaTypes">  
             <list>  
                 <value>text/html;charset=UTF-8</value>  
                 <value>application/json; charset=UTF-8</value>  
             </list>  
         </property>  
        </bean>

objectMapper

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

public class CmsObjectMapper extends ObjectMapper {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5750780864051371603L;

	public CmsObjectMapper() {
//		super();
		// 设置null值不参与序列化(字段不被显示)
		this.setSerializationInclusion(Include.NON_NULL);

		// 禁用空对象转换json校验
//		this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
		this.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
		// 驼峰命名法转换为小写加下划线
//		this.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
		this.setPropertyNamingStrategy(new CmsPropertyNamingStrategy());
	}
}

namingStrategy

import org.springframework.util.StringUtils;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;

public class CmsPropertyNamingStrategy extends PropertyNamingStrategy {

    /**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 5849045537453221193L;

	@Override
    public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
        return withoutStrikethroughName(defaultName);
    }

    @Override
    public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
        return withoutStrikethroughName(defaultName);
    }

    @Override
    public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
        return withoutStrikethroughName(defaultName);
    }

    @Override
    public String nameForConstructorParameter(MapperConfig<?> config, AnnotatedParameter ctorParam,
                                              String defaultName) {
        return withoutStrikethroughName(defaultName);
    }

    private String withoutStrikethroughName(String name) {
    	String result = name;
    	
        if (StringUtils.isEmpty(name)) {
        	result = "";
        }
        if(name != null && name.toLowerCase().contains("lipage")) {
        	System.out.println();
        }
    	
        if (name.contains("-")) {
        	result = name.replaceAll("-", "");
        }
        
        if(result != null) {
            result = result.substring(0, 1).toLowerCase() + result.substring(1);
        }
        
        return result;
    }

    private String withoutUnderscoreName(String name) {
        if (StringUtils.isEmpty(name)) {
            return "";
        }
        StringBuilder result = new StringBuilder();
        result.append(name.substring(0, 1).toLowerCase());
        boolean underscore = false;
        for (int i = 1; i < name.length(); ++i) {
            String s = name.substring(i, i + 1);
            if ("_".equals(s)) {
                underscore = true;
                continue;
            } else {
                if (underscore) s = s.toUpperCase();
                underscore = false;
            }
            result.append(s);
        }
        return result.toString();
    }
}


Spring Message Converter

标签:springconv   spring converter   

原文地址:http://blog.51cto.com/xinzhilian/2052836

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!