liuxiaolong
2019-05-06 a7bed6b4cfecd61ec153818945f982c5bb796b98
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.cloud.attendance.config;
 
import java.util.HashMap;
import java.util.Map;
 
import lombok.extern.log4j.Log4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
 
@Log4j
@RestControllerAdvice
public class ExceptionHandlerAdvice {
 
    @ExceptionHandler({ IllegalArgumentException.class })
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Map<String, Object> badRequestException(IllegalArgumentException exception) {
        log.error(exception);   // wp 2018-12-15 添加
        exception.printStackTrace();
        Map<String, Object> data = new HashMap<>();
        data.put("code", HttpStatus.BAD_REQUEST.value());
        data.put("msg", exception.getMessage());
        data.put("success",false);
        data.put("data",null);
        return data;
    }
 
 
    @ExceptionHandler({Exception.class, Throwable.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String, Object> serverException(Throwable throwable) {
        log.error("服务端异常", throwable);  //
        Map<String, Object> data = new HashMap<>();
        data.put("code", HttpStatus.INTERNAL_SERVER_ERROR.value());
        data.put("msg", "服务端异常,请联系管理员 ,错误信息:"+throwable.getLocalizedMessage());
        data.put("success",false);
        data.put("data",null);
        return data;
    }
 
    @ExceptionHandler({ SecurityException.class })
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Map<String, Object> badRequestException(SecurityException exception) {
        log.info(exception);
        Map<String, Object> data = new HashMap<>();
        data.put("code", HttpStatus.UNAUTHORIZED.value());
        data.put("msg", exception.getMessage());
        data.put("success",false);
        data.put("data",null);
        return data;
    }
}