zoukankan      html  css  js  c++  java
  • springmvc配置MappingJackson2HttpMessageConverter实现属性驼峰和下划线的转换

    需求

      php调用java接口时,因为php那边的属性都是下划线风格,java这边的属性都是驼峰的风格。配置springmvc的json转换,在requestBody的时候(调用对象的set 方法)将java属性name映射成下划线形式 和 请求的参数匹配;在responseBody的时候(调用对象的get方法)将java的属性name也映射成下划线形式。

    MappingJackson2HttpMessageConverter配置

    <mvc:annotation-driven>
            <mvc:message-converters>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="objectMapper">
                        <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                            <!-- 处理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.xxx.serializer.MyPropertyNamingStrategyBase" />-->
                                <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>
            </mvc:message-converters>
        </mvc:annotation-driven>

     自定义PropertyNamingStrategy

    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;
    import org.apache.commons.lang3.StringUtils;
    
    /**
     * Created by hujunzheng on 2017/6/7.
     */
    public class MyPropertyNamingStrategyBase extends PropertyNamingStrategy {
    
        @Override
        public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
            return defaultName;
        }
    
        @Override
        public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
            return underscoreName(defaultName);
        }
    
        @Override
        public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
            return underscoreName(defaultName);
        }
    
        @Override
        public String nameForConstructorParameter(MapperConfig<?> config, AnnotatedParameter ctorParam,
                                                  String defaultName) {
            return defaultName;
        }
    
        private String underscoreName(String name) {
            if (StringUtils.isEmpty(name)) {
                return "";
            }
            StringBuilder result = new StringBuilder();
            result.append(name.substring(0, 1).toLowerCase());
            for (int i = 1; i < name.length(); ++i) {
                String s = name.substring(i, i + 1);
                String slc = s.toLowerCase();
                if (!(s.equals(slc))) {
                    result.append("_").append(slc);
                } else {
                    result.append(s);
                }
            }
            return result.toString();
        }
    
        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();
        }
    }
  • 相关阅读:
    Java 获取字符串指定下标位置的值 charAt()
    Java 获取字符串长度 length()
    Java 字符串拼接 StringBuilder() StringBuffer
    ngBind {{}} ngBindTemplate
    什么是:before和:after?
    滚屏加载
    JavaScript 高程三读书笔记;
    angularjs 构建主页 内置过滤器、日期的格式化
    Angular实现递归指令
    JQuery获取浏览器窗口的可视区域高度和宽度,滚动条高度
  • 原文地址:https://www.cnblogs.com/hujunzheng/p/6959182.html
Copyright © 2011-2022 走看看