- 定义一个公共的返回数据类
package com.xiaohan;
import java.util.HashMap;
import java.util.Map;
/**
* 返回数据
*/
public class R extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
// 定义一个枚举 定义 错误码跟错误信息
public enum Code {
OK(0, null), ERROR(-1, "未知异常,请联系管理员");
private int code;
private String msg;
private Code(int code, String msg) {
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
public R() {
put("code", Code.OK.getCode());
}
public static R error() {
return error(Code.ERROR.getCode(), Code.ERROR.getMsg());
}
public static R error(String msg) {
return error(Code.ERROR.getCode(), msg);
}
public static R error(int code, String msg) {
R r = new R();
r.put("code", code);
r.put("msg", msg);
return r;
}
public static R ok(String msg) {
R r = new R();
r.put("msg", msg);
return r;
}
public static R ok(Map<String, Object> map) {
R r = new R();
r.putAll(map);
return r;
}
public static R ok() {
return new R();
}
public R put(String key, Object value) {
super.put(key, value);
return this;
}
}
- 定义一个控制器通知
package com.xiaohan.controller.advice;
import com.xiaohan.R;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
// 控制器通知
@RestControllerAdvice
public class ExceptionAdvice {
// 全局处理指定的异常类型
@ExceptionHandler(Exception.class)
public R exceptionHandler(Exception e) {
String message = e.getMessage();
return R.error(message);
}
// 添加 键值对 到Model中
@ModelAttribute
public void addAttributes(Model model) {
model.addAttribute("msg", "额外信息");
}
// 修改Date类型的参数 绑定模式
@InitBinder
public void initBinder1(WebDataBinder binder) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setLenient(true);
// 参数绑定 忽略id属性
binder.setDisallowedFields("id");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
}
- 定义一个控制器
package com.xiaohan.controller;
import com.xiaohan.R;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Date;
@RestController
public class AdviceController {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@RequestMapping("/advice")
public R getSomething(@ModelAttribute("msg") String msg, AdviceController adviceController,String id, Date date) {
System.err.println(adviceController.getId()); // null
System.err.println(id); // 1
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
System.err.println(date == null ? date : sdf.format(date)); //20170829
throw new RuntimeException(msg);
}
}
- 浏览器请求响应结果
请求地址
http://localhost/advice?id=1&date=2017-08-29 23:45:00
响应结果
{"msg":"额外信息","code":-1}
输出
null
1
20170829
可以看到方法参数上的id参数并没有忽略值 但是对象中的id属性 是null值
网友评论