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());
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构