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());
    }
}