温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Message Converter

发布时间:2020-07-24 17:37:27 来源:网络 阅读:1673 作者:xiaosawuhen 栏目:开发技术

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();
    }
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI