当前位置:网站首页>JSON工具类
JSON工具类
2022-07-21 05:02:00 【牛牛的Java学习之旅】
JSON工具类
package com.liu.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.text.SimpleDateFormat;
public class JsonUtils {
//传一个参数
public static String getJson(Object object){
return getJson(object,"yyyy-MM-dd HH:mm:ss");
}
//传两个参数
public static String getJson(Object object,String dateFormat){
ObjectMapper mapper = new ObjectMapper();
//关闭时间戳的方式
mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
mapper.setDateFormat(sdf);
try {
return mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
}
测试
@RestController//不会走视图解析器
public class UserController {
@RequestMapping("/j1")
public String json1(){
//创建一个对象
User user = new User("刘想",3,"男");
return JsonUtils.getJson(user);
}
结果
配置好tomcat,输入url测试
url:(http://localhost:8080/springmvc_05_json_war_exploded/j1)
结果:
边栏推荐
- Odoo God operation background call routing interface
- 让你告别996的项目维护方式
- 867. Decomposition prime factor
- My understanding of UI automated testing
- Let you say goodbye to the project maintenance mode of 996
- Written test compulsory training day 18
- ES6 related interview questions
- Sword finger offer special assault version day 5
- Fluent customizes form and encapsulates form components
- 单体项目-瑞吉外卖
猜你喜欢
ImportError: DLL load failed while importing win32gui: 找不到指定的模块。
让你告别996的项目维护方式
STM32 - ADC reads the photosensitive sensor, controls the LED light, and the watchdog interrupts
FL Studio 20.9水果编曲软件中文汉化补丁包
1451 - Cannot delete or update a parent row 具有多个外键约束 删除子表数据行的问题
(四)事务
Introduction, installation and basic use of MYCAT
MyCat的介绍与安装以及基本使用
uniapp开发app解决uni.pageScrollTo不生效
图表即代码:以代码化的方式构建新一代图形库 —— Feakin
随机推荐
Let you say goodbye to the project maintenance mode of 996
自动抓包工具 web scraper
867. Decomposition prime factor
ES6的数组、对象拷贝
10 lines of code to realize wechat applet payment function, and use applet cloud development to realize applet payment function (including source code
Uniapp develops apps to solve uni Pagescrollto does not take effect
Error in pulling project POM file, Jai_ core-1.1.3. Jar, solve the problem of introducing jar
NXP i.MX 8m mini core board specifications, quad core arm cortex-a53 + arm cortex-m4
URL 和 URI
2022.7.21DAY611
STM32 - positioning module atgm336h, data analysis, longitude and latitude extraction
How do I optimize from 20s to 500ms
Okaleido tiger NFT is about to log in to binance NFT platform, and the future market continues to be optimistic
Boundary value analysis of common test case design methods
MySQL之DML(数据操纵语言)
In fastjason data type, there is a problem of $ref: "$.list[0]" when parsing jsonobject
MySQL之DCL(数据控制语言)
拉取项目pom文件报错,jai_core-1.1.3.jar,解决jar引入问题
Interview with data center and Bi business (II): pit of organizational structure Sorting
866. 试除法判定质数