导航:首页 > 编程语言 > springjson格式化

springjson格式化

发布时间:2023-03-21 19:56:42

Ⅰ 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 的格式了。

阅读全文

与springjson格式化相关的资料

热点内容
专题学习网站源码 浏览:163
jsphead什么 浏览:88
gps串口数据怎么发送 浏览:968
win10文件主页共享查看 浏览:411
中国联通有哪些app是免流的 浏览:176
边做边保存的文件找不到了 浏览:858
win10照片应用文件夹名称 浏览:966
编程如何解决资金的原子性 浏览:638
如何制作广角镜头矫正文件 浏览:513
在网页开发中应该选用哪个数据库 浏览:742
iphone5移动卡贴 浏览:990
电脑文件的格式 浏览:127
extjs的xtype 浏览:959
suse11iso文件要u盘安装 浏览:153
如何将报表统计数据转化为图形 浏览:444
如何寄快递材料文件 浏览:265
java构造方法private 浏览:475
手机文件找回恢复 浏览:516
word怎么把u盘里的文件拔掉 浏览:976
港版苹果用的插排 浏览:1000

友情链接