Spring Message Converter
疑问点:NamingStrategy是对于json的name修改,还是javabean的name修改???
其他的MessageConterver
MappingJackson2HttpMessageConverter:
XML
NON_NULL
text/html;charset=UTF-8 application/json; charset=UTF-8
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(); }}