美文网首页
Spring 全局异常、数据绑定

Spring 全局异常、数据绑定

作者: 胖嘟嘟洒酒疯 | 来源:发表于2019-01-16 18:52 被阅读0次

全局异常处理

异常处理类配置

@ControllerAdvice
public class ControllerAdviceConfig {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public String sessionNotFoundExceptionHandler(HttpServletRequest request, Exception e) throws Exception {
        return "Controller Exception handle";
    }
}

Controllere层

    @GetMapping("/exception/{id}")
    public String exception(@PathVariable("id") Integer id) throws Exception {
        if (1 == id) {
            throw new Exception("异常");
        }
        return "exception";
    }

参数数据解析

在配置中添加

    @InitBinder
    public void initBinder(WebDataBinder webDataBinder) {
        webDataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() {

            @Override
            public void setAsText(String text) {
                try {
                    setValue(new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").parse(text));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public String getAsText() {
                return new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(getValue());
            }
        });
    }

相关文章

网友评论

      本文标题:Spring 全局异常、数据绑定

      本文链接:https://www.haomeiwen.com/subject/xrqvdqtx.html