Ⅰ Spring Boot使用@jsonProperty,@JsonIgnore,@JsonFormat注解
@JsonProperty, @JsonIgnore 和 @JsonFormat 注解都是 fasterxml jackson 里面的注解,现在也被 Spring Boot 集成了。
这里需要注意的是将对象转换成json字符串使用的方法是fasterxml.jackson提供的!!
如果使用fastjson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
没有生效,为啥?
因为fastjson不认识@JsonProperty注解呀!所以要使用jackson自迟埋己的序列化工具方法
我们在使用上面的注解时,不需要在 pom.xml 显示的引入 fasterxml jackson 的依赖包。只需要加入如下依赖即可。
@JsonProperty
用于属性、setter / getter 方法上,属性序列化后可重命名
生成的 json 字符串就是image_width和image_height。
@JsonIgnore
属性使用此注解后,将不被序列化。
@JsonFormat
用于格式化日期
@JsonInclude,@JsonIgnoreProperties,@JsonIgnore
真实案例
{
"rowid": "111111",
"created": "液清2018-12-27 16:15:25",
"createdby": "1111111",
"lastupd": "2018-12-27 08:25:48",
"lastupdby": "111111",
"modificationnum": 1
}
返回Json参数字段均为小写,在接收时,需要按照标准的命名规则进行映射
解决办法:
创建接收数据对象,生成Get\Set方法:,在Set方法上,加上@JsonProperty注解,
@JsonProperty 此注解用于属性上,作用是把该属性的名称序列化为另闹旦前外一个名称,如把rowId属性序列化为rowid,@JsonProperty("rowid")。
Ⅱ SpringMVC项目如何全局格式化日期格式
spring的RequestMappingHandlerAdapter中的messageConverters就能够全局格式化日期格式
1. 配置spring-servlet
- 引入请求映射器
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
- 加入消息转化器
messageConverters
- 引入Json转化器
org.springframework.http.converter.json.
- 实现对象转化器
objectMapper
- 日期转换格式定义
dateFormat
2. 代码
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.suixingpay.common.web.WebBindingInitializer">
<property name="conversionService">
<bean class="org.springframework.format.support."></bean>
</property>
</bean>
</property>
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean class="org.springframework.http.converter.json.">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>
</property>
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>text/plain;charset=UTF-8</value>
<value>text/json;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
Ⅲ spring mvc4.x怎么返回json格式
springmvcajax返回json字符串的设置方法:使用技术及环境:Spring3.2.2.RELEASEJackson1.9.10JDK1.6Eclipse3.6Maven3PS:在spring3中,要输出json数据,只需要添加Jackson 库到你的classpath。1、项目依赖spring和jackson的依赖:4.0.0com.mkyong.commonSpringMVCwar1.0-.model.Shop;@Controller@RequestMapping("/kfc/brands")publicclassJSONController{@RequestMapping(value="{name}",method=RequestMethod.GET)public@ResponseBodyShopgetShopInJSON(@PathVariableStringname){Shopshop=newShop();shop.setName(name);shop.setStaffName(newString[]{"mkyong1","mkyong2"});returnshop;}}4、mvc:annotation-driven在你的spring配置文件中启用mvc:annotation-driven注解。5、示例结果
Ⅳ Spring mvc 返回json数组中的日期怎么格式化
只要继承它的抽象类:public abstract class JsonSerializer<T>,并在相应的属性方法上添加指定注解:@JsonSerialize 即可实现。
编写Date日志自定义转换类:
CustomDateSerializer.java
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
public class CustomDateSerializer extends JsonSerializer<Date> {
@Override
public void serialize(Date value, JsonGenerator jgen,SerializerProvider provider)
throws IOException,JsonProcessingException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = formatter.format(value);
jgen.writeString(formattedDate);
}
}
javabean对应属性的方法添加注解:
@JsonSerialize(using = CustomDateSerializer.class)
public Date getCreateDate() {
return createDate;
}
就这么简单就可以实现返回的JSON数据中日期格式自动转换为:yyyy-MM-dd HH:mm:ss 的格式了。