springboot默认的json配置
springboot默认的json配置
1.@JsonIgnore
返回前端时对应字段不进行序列化返回
public class User {
@JsonIgnore
private String name;
}
2.@JsonFormat
日期格式化
public class User {
@JsonFormat(pattern = "yyyy-mm-dd HH:mm:ss")
private String date;
}
3.@JsonInclude
满足某种条件返回
public class User {
@JsonInclude(JsonInclude.Include.NON_NULL)
private String name;
}
4.@JsonProperty
指定字段的别名
public class User {
@JsonProperty("agx")
private String age;
}
Json国际化
在springboot的resources文件下创建i18n文件夹
文件夹下创建三个文件
1.messages.properties
user.query.success=查询成功
2.messages_en_US.properties
user.query.success=query success
3.messages_zh_CN.properties
user.query.success=查询成功
4.application.yml
spring:
messages:
basename: i18.message
5.书写测试Controller
package com.wangfan.controller;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class TestAAController {
@Resource
private MessageSource messageSource;
@GetMapping("/aa")
public String aa(){
return messageSource.getMessage("user.query.success", null, LocaleContextHolder.getLocale());
}
}
SpringBoot同一异常处理
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.method.HandlerMethod;
@ControllerAdvice
public class ExceptionDealController {
@ExceptionHandler(Exception.class)
public void handler(Exception ex, HandlerMethod method){
System.out.println("统一异常处理!");
System.out.println(ex.getMessage());
System.out.println(method.getBean().getClass());
System.out.println(method.getMethod().getName());
}
}
springboot默认的json配置的更多相关文章
- SpringBoot 默认json解析器详解和字段序列化自定义
前言 在我们开发项目API接口的时候,一些没有数据的字段会默认返回NULL,数字类型也会是NULL,这个时候前端希望字符串能够统一返回空字符,数字默认返回0,那我们就需要自定义json序列化处理 Sp ...
- 【2.0】使用默认日志slf4j配置
一.SpringBoot日志简介 SpringBoot使用Commons Logging进行所有内部日志记录,但底层日志实现保持开放状态.为Java Util Logging,Log4j2和Logba ...
- springBoot日志框架自动配置与原理
1.日志框架 小张:开发一个大型系统: 1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件? 2.框架来记录系统的一些运行时信息: ...
- Springboot 禁用数据库自动配置
转载至:https://blog.csdn.net/wyw815514636/article/details/80846545 https://blog.csdn.net/knqi007/articl ...
- springboot核心技术(二)-----自动配置原理、日志
自动配置原理 配置文件能配置的属性参照 1.自动配置原理: 1).SpringBoot启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfiguration 2).@Enable ...
- spring boot @ResponseBody转换JSON 时 Date 类型处理方法,Jackson和FastJson两种方式,springboot 2.0.9配置fastjson不生效官方解决办法
spring boot @ResponseBody转换JSON 时 Date 类型处理方法 ,这里一共有两种不同解析方式(Jackson和FastJson两种方式,springboot我用的1.x的版 ...
- 配置和修改springboot默认国际化文件
SpringBoot默认国际化文件为:classpath:message.properties,如果放在其它文件夹中,则需要在application.properties配置属性spring.mess ...
- SpringBoot默认日志的使用方法及常用配置
SpringBoot默认采用slf4j+logback 的组合形式,但也提供对JUL.log4j2.Logback提供了默认配置. 我们使用IDEA的spring初始化创建一个springboot项目 ...
- springboot情操陶冶-web配置(四)
承接前文springboot情操陶冶-web配置(三),本文将在DispatcherServlet应用的基础上谈下websocket的使用 websocket websocket的简单了解可见维基百科 ...
- springboot情操陶冶-web配置(三)
承接前文springboot情操陶冶-web配置(二),本文将在前文的基础上分析下mvc的相关应用 MVC简单例子 直接编写一个Controller层的代码,返回格式为json package com ...
随机推荐
- jsp <img src="“> src 相对路径的问题
图片所在路径 jsp所在路径 调用处 相对路径 以调用处JSP位置为参考 每一个../代表上一级 4个 正好找到到webRoot 目录拼接后 webRoot /kernel/_static/img/ ...
- python38
Python break 语句 Python break语句,就像在C语言中,打破了最小封闭for或while循环. break语句用来终止循环语句,即循环条件没有False条件或者序列还没被完全递归 ...
- java script--一些冷知识
java script--一些冷知识 目录 java script--一些冷知识 布兰登·艾奇,JavaScript 创始人 java script logo json logo json node. ...
- Jmeter - Config Tips
Guideline: Jmeter.properties file should avoid to be modified , modified user.properties instead > ...
- vue store用法
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) 首字母不能大写 export default new Vuex.Store({ ...
- unity tex2Dlod in vert
https://forum.unity.com/threads/how-to-sample-a-texture-in-vertex-shader.513816/ GreatWall said: ↑ ...
- HTML弹窗设计二
<!DOCTYPE html><html> <head> <title>模态框弹出层.html</title> <meta http- ...
- D2-Net: Weakly-Supervised Action Localization via Discriminative Embeddings and Denoised Activations概述
1.针对的问题 目前大多数弱监督动作定位方法通常依赖于分离前景和背景区域(前-背景分离)学习TCAMs,但是在弱监督设置下,学习到的TCAM会存在噪声,而这些方法并没有明确地处理其噪声输出. 2.主要 ...
- 利用position: absolute最简便实现水平居中的css样式
html <div class="horizontal-center"></div> css .horizontal-center { left: 50%; ...
- zabbix 使用小技巧
001.监控项指定时间执行 wd1-7h01m00 周一到周日,每天 01:00:00 执行一次 002.监控项某一段时间点每分钟执行一次 1-5,00:00-09:00 6-7,00:00-23:5 ...