导航:首页 > 编程语言 > 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格式化相关的资料

热点内容
jsp本地路径 浏览:615
对不符合要求的设计文件如何管控 浏览:812
如何阻止推送以及app间关联 浏览:779
qq如何设置不自动发送离线文件 浏览:815
文件夹隐藏恢复 浏览:162
网易大神逆水寒捏脸数据怎么用 浏览:309
华三交换机的网络克隆是什么原理 浏览:232
仙剑那个版本好玩 浏览:231
华为人体秤不更新数据怎么办 浏览:1
qq红包密码设置在哪里 浏览:291
批量删除文件linux 浏览:545
html5怎么推送摄像头数据流 浏览:893
如何将7Z压缩文件改格式 浏览:17
c程序设计基础周霭如pdf 浏览:1000
我的世界怎么强制疾跑编程 浏览:434
飞歌大众g8安卓导航 浏览:436
java字符串自动补0 浏览:60
qq呼叫别人没有声音 浏览:472
育儿网站怎么下载 浏览:969
加工床头箱如何编程 浏览:611

友情链接