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配置的更多相关文章

  1. SpringBoot 默认json解析器详解和字段序列化自定义

    前言 在我们开发项目API接口的时候,一些没有数据的字段会默认返回NULL,数字类型也会是NULL,这个时候前端希望字符串能够统一返回空字符,数字默认返回0,那我们就需要自定义json序列化处理 Sp ...

  2. 【2.0】使用默认日志slf4j配置

    一.SpringBoot日志简介 SpringBoot使用Commons Logging进行所有内部日志记录,但底层日志实现保持开放状态.为Java Util Logging,Log4j2和Logba ...

  3. springBoot日志框架自动配置与原理

    1.日志框架 小张:开发一个大型系统: ​ 1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件? ​ 2.框架来记录系统的一些运行时信息: ...

  4. Springboot 禁用数据库自动配置

    转载至:https://blog.csdn.net/wyw815514636/article/details/80846545 https://blog.csdn.net/knqi007/articl ...

  5. springboot核心技术(二)-----自动配置原理、日志

    自动配置原理 配置文件能配置的属性参照 1.自动配置原理: 1).SpringBoot启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfiguration 2).@Enable ...

  6. spring boot @ResponseBody转换JSON 时 Date 类型处理方法,Jackson和FastJson两种方式,springboot 2.0.9配置fastjson不生效官方解决办法

    spring boot @ResponseBody转换JSON 时 Date 类型处理方法 ,这里一共有两种不同解析方式(Jackson和FastJson两种方式,springboot我用的1.x的版 ...

  7. 配置和修改springboot默认国际化文件

    SpringBoot默认国际化文件为:classpath:message.properties,如果放在其它文件夹中,则需要在application.properties配置属性spring.mess ...

  8. SpringBoot默认日志的使用方法及常用配置

    SpringBoot默认采用slf4j+logback 的组合形式,但也提供对JUL.log4j2.Logback提供了默认配置. 我们使用IDEA的spring初始化创建一个springboot项目 ...

  9. springboot情操陶冶-web配置(四)

    承接前文springboot情操陶冶-web配置(三),本文将在DispatcherServlet应用的基础上谈下websocket的使用 websocket websocket的简单了解可见维基百科 ...

  10. springboot情操陶冶-web配置(三)

    承接前文springboot情操陶冶-web配置(二),本文将在前文的基础上分析下mvc的相关应用 MVC简单例子 直接编写一个Controller层的代码,返回格式为json package com ...

随机推荐

  1. UI工具

    sketch figma Adobe Photoshop Adobe Illustrator adobe xd

  2. ubuntu - 程序运行的一些基础知识

    1.指定编码格式 -finput-charset=GB2312 指定 C 程序的编码方式为 GB2312,不指定时默认编码方式为 UTF-8 -fexec-charset=GB2312 指定 可执行程 ...

  3. OpenMP fortran 学习

    参考自TAMU的PPThttps://people.math.umass.edu/~johnston/PHI_WG_2014/OpenMPSlides_tamu_sc.pdf 什么是OpenMP 在C ...

  4. 微信小程序笔记_02

    在微信小程序中使用Echarts组件 github源码地址:https://github.com/ecomfe/echarts-for-weixin gitcode源码地址:https://gitco ...

  5. 用xlutils.copy写入中文的问题

    用xlutils.copy 将中文写入excel文档中 遇到的问题1: Traceback:Traceback (most recent call last):File "C:\Users\ ...

  6. 如何使用cmd(dos命令)关闭IIS中某个站点

    在 目录  C:\Windows\System32\inetsrv 下面有一个 appcmd 程序,定位到 该目录下 appcmd site /?  #管理站点 appcmd /? #管理整个IIS ...

  7. composer更新报错

    在用composer安装Yii2的扩展插件时,报错: [UnexpectedValueException] Could not parse version constraint <=2.*: I ...

  8. 根据XML生成实体类

    因为对接系统的XML所需映射的实体类有几十个,自己来处理不太现实,于是一直找寻找这样的工具,终于让我发现了jaxb2-maven-plugin: http://www.mojohaus.org/jax ...

  9. C#中的ReferenceEquals、Equals以及==

    https://www.cnblogs.com/ArtofDesign/p/3615212.html   C#中有一共有四种相等性判断方法: //Object中定义的三个方法 public stati ...

  10. ref、reactive、toRef、toRefs使用与区别

    reactive 传参:reactive(arg),arg只能是对象 arg为普通对象 返回响应式对象,不管层级多深,都能响应 使用:获取数据值的时候直接获取,不需要加.value 特点:解构.扩展运 ...