使用步骤:
一)在项目中添加依赖:
二)要求Controller的方法的返回值可以是java任意类型,
但是SpringMVC默认把Controller方法返回值作为作为逻辑视图名,
所以我们要在方法上使用@ResponseBody注解 :
告诉springMVC 这个方法返回值是以数据形式响应给前端,
而且如果你添加了json相关依赖,springMVC会自动把java对象===>json格式字符串,响应给前端
eg1:
eg2:
====>Controller的方法参数写实体类时,要保证前端传来的属性名和实体类属性名一样
使用jackson时日期会转换成毫秒值,要指定格式时要配置springMVC.xml
TODO:复制springMVC.xml配置日期格式代码
<!--基于注解的处理器映射器,处理器适配器
conversion-service: 使用转换器服务的bean, 就是配置FormattingConversionServiceFactoryBean的id
-->
<mvc:annotation-driven conversion-service="conversionService">
<!--jackson相关配置
使用子标签: message-converters
jackson提供了这个转换器 MappingJackson2HttpMessageConverter
-->
<mvc:message-converters>
<!--jackson提供了这个转换器 MappingJackson2HttpMessageConverter
内部bean, 范围在包含的标签内部有效
-->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<!--配置metaType 以及编码
json对应的metaType
-->
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<!--对java对象转换的相关配置
ObjectMapper: 自定义一个类
jackson提供这个类
-->
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<!--日期转换器: SimpleDateFormat 是DateFormat的子类-->
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<!--jackson 把java的date类型的属性转换为yyyy-MM-dd的字符串给前端-->
<constructor-arg name="pattern" value="yyyy-MM-dd"/>
</bean>
</property>
<!--对null忽略: 属性的值为null, 不会这个属性响应给前端
serializationInclusion: 设置序列化包含的内容,
值: Include的枚举类型
public static enum Include {
ALWAYS, 默认值
NON_NULL, 把不为null响应给前端
NON_ABSENT,
NON_EMPTY,
NON_DEFAULT,
CUSTOM,
USE_DEFAULTS;
枚举类型的值的配置
-->
<property name="serializationInclusion">
<!-- type: 包.枚举类
value标签之间写的枚举名
-->
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
<!--配置json的属性名的取名策略: 默认json的属性名就是java的属性名, 一般也是采用这种策略
propertyNamingStrategy:
java对象属性名: json的name
默认策略 userName userName
SnakeCaseStrategy: userName user_Name
UpperCamelCaseStrategy: userName UserName
LowerCaseStrategy: userName username
KebabCaseStrategy: userName user-name
LowerDotCaseStrategy: userName user.name
-->
<!-- <property name="propertyNamingStrategy">
<bean class="com.fasterxml.jackson.databind.PropertyNamingStrategy$KebabCaseStrategy"></bean>
</property>-->
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
====>让前端对空会忽略:后台会传递给前端一些null值,前端不想要null值,就可以设置对空忽略
TODO:补充对空忽略代码,代码在springMVC.xml中
<!--对null忽略: 属性的值为null, 不会这个属性响应给前端
serializationInclusion: 设置序列化包含的内容,
值: Include的枚举类型
public static enum Include {
ALWAYS, 默认值
NON_NULL, 把不为null响应给前端
NON_ABSENT,
NON_EMPTY,
NON_DEFAULT,
CUSTOM,
USE_DEFAULTS;
枚举类型的值的配置
-->
<property name="serializationInclusion">
<!-- type: 包.枚举类
value标签之间写的枚举名
-->
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
涉及对枚举类型赋值
<!--对null忽略: 属性的值为null, 不会这个属性响应给前端
serializationInclusion: 设置序列化包含的内容,
值: Include的枚举类型
public static enum Include {
ALWAYS, 默认值
NON_NULL, 把不为null响应给前端
NON_ABSENT,
NON_EMPTY,
NON_DEFAULT,
CUSTOM,
USE_DEFAULTS;
枚举类型的值的配置
-->
<property name="serializationInclusion">
<!-- type: 包.枚举类
value标签之间写的枚举名
-->
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>