首页 > 其他分享 >springboot默认的json配置

springboot默认的json配置

时间:2023-02-26 22:48:03浏览次数:32  
标签:springboot springframework class json query 默认 org import public

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,springframework,class,json,query,默认,org,import,public
From: https://www.cnblogs.com/WangJingjun/p/17158023.html

相关文章

  • JSON注入
    JSON注入原理:JSON注入是指应用程序所解析的JSON数据来源于不可信赖的数据源,程序没有对这些不可信赖的数据进行验证、过滤,如果应用程序使用未经验证的输入构造JSON,则可以......
  • ES6-ES11 函数参数的默认值设置
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>函数参......
  • 阻止浏览器默认行为和防止事件传播
    阻止浏览器默认行为和防止事件传播主要通过先下面两个方法实现event.preventDefault(): 取消浏览器对当前事件的默认行为,比如点击链接后,浏览器跳转到指定页面,或者按一下空......
  • go结构体打印格式化成json
    需要用到json.MarshalIndent方法官方的注释是:MarshalIndent类似于Marshal,但应用Indent来格式化输出。输出中的每个JSON元素都将以一个新行开始,该新行以前缀开头,后跟......
  • json.load()与json.loads(),json.dump()与json.dumps()的区别,一目了然!!!
    引言很多时候,我们都会把json.load()与loads(),json.dump()与dumps()方法弄混淆,包括我也是,但是面试又会经常面试到。为了一次性把这两对方法的区别说清楚,我经过思考和对......
  • 03_19_JavaWeb||day22_Ajax&Json||day22_Ajax&Json
    今日内容1.AJAX:增强用户体验2.JSON:一种数据格式1.AJAX:概念:ASynchronousJavaScriptAndXML异步的JavaScript和XML异步和同步:客户端和服务器端相互通信的基础上......
  • python基础-json
    importjson#准备列表,列表内每一个元素都是字典,将其转为JSONdate=[{"name":"张大帅","age":11},{"name":"王大锤","age":13},{"name":"赵......
  • SpringBoot内置tomcat参数调优
    1.默认配置可通过org.springframework.boot.autoconfigure.web.ServerProperties查看,其中包括属性tomcat、jetty、undertow三种服务器的设置,默认启用tomcat。#tomcat......
  • 【springboot】约定优于配置
    spring的核心思想:约定优于配置 @SpringBootApplication这个注解的本质是有以下三个注解1.@SpringBootConfiguration表示该类是一个配置类2.@EnableAutoCon......
  • 接收json数据
    1.导入坐标:<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.0</version></dependency>2.......