springboot configuration global exception

The first step in the controller

package cn.itcast.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.itcast.springboot.pojo.IMoocJSONResult;

 

@Controller
@RequestMapping("err")
public class ErrorController {

@RequestMapping("/error")
public String error() {

int a = 1 / 0;

return "thymeleaf/error";
}

@RequestMapping("/ajaxerror")
public String ajaxerror() {

return "thymeleaf/ajaxerror";
}

@RequestMapping("/getAjaxerror")
@ResponseBody
public IMoocJSONResult getAjaxerror() {

int a = 1 / 0;

return IMoocJSONResult.ok();
}
}

The referenced class has already been written.

package cn.itcast.springboot.pojo;

import java.util.List;


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
*
* @Title: LeeJSONResult.java
* @Package com.lee.utils
* @Description: Custom response data structure
* This class is provided to the portal, ios, Android, WeChat mall
* The portal accepts such data Afterwards, you need to use the methods of this class to convert to the corresponding data type format (class, or list)
* Other processing
* 200: indicates success
* 500: indicates error, the error message is in the msg field
* 501: bean validation error, no matter how many All errors are returned in the form of map
* 502: Interceptor intercepted user token error
* 555: Exception thrown information
* Copyright: Copyright (c) 2016
* Company: Nathan.Lee.Salvatore
*
* @author leechenxiang
* @date 2016 Apr 22 8:33:36 PM
* @version V1.0
*/
public class IMoocJSONResult {

// Define jackson object
private static final ObjectMapper MAPPER = new ObjectMapper();

// Response to business status
private Integer status;

// response message
private String msg;

// data in the response
private Object data;

private String ok; // not used

public static IMoocJSONResult build(Integer status, String msg, Object data) {
return new IMoocJSONResult(status, msg, data);
}

public static IMoocJSONResult ok(Object data) {
return new IMoocJSONResult(data);
}

public static IMoocJSONResult ok() {
return new IMoocJSONResult(null);
}

public static IMoocJSONResult errorMsg(String msg) {
return new IMoocJSONResult(500, msg, null);
}

public static IMoocJSONResult errorMap(Object data) {
return new IMoocJSONResult(501, "error", data);
}

public static IMoocJSONResult errorTokenMsg(String msg) {
return new IMoocJSONResult(502, msg, null);
}

public static IMoocJSONResult errorException(String msg) {
return new IMoocJSONResult(555, msg, null);
}

public IMoocJSONResult() {

}

// public static LeeJSONResult build(Integer status, String msg) {
// return new LeeJSONResult(status, msg, null);
// }

public IMoocJSONResult(Integer status, String msg, Object data) {
this.status = status;
this.msg = msg;
this.data = data;
}

public IMoocJSONResult(Object data) {
this.status = 200;
this.msg = "OK";
this.data = data;
}

public Boolean isOK() {
return this.status == 200;
}

public Integer getStatus() {
return status;
}

public void setStatus(Integer status) {
this.status = status;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public Object getData() {
return data;
}

public void setData(Object data) {
this.data = data;
}

/**
*
* @Description: Convert the json result set to a LeeJSONResult object
* The object to be converted is a class
* @param jsonData
* @param clazz
* @return
*
* @author leechenxiang
* @date 22nd April 2016 afternoon 8:34:58
*/
public static IMoocJSONResult formatToPojo(String jsonData, Class<?> clazz) {
try {
if (clazz == null) {
return MAPPER.readValue(jsonData, IMoocJSONResult.class);
}
JsonNode jsonNode = MAPPER. readTree(jsonData);
JsonNode data = jsonNode.get("data");
Object obj = null;
if (clazz != null) {
if (data.isObject()) {
obj = MAPPER.readValue(data.traverse() , clazz);
} else if (data.isTextual()) {
obj = MAPPER.readValue(data.asText(), clazz);
}
}
return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
} catch (Exception e) {
return null;
}
}

/**
*
* @Description: No object conversion
* @param json
* @return
*
* @author leechenxiang
* @date April 22, 2016 8:35:21 PM
*/
public static IMoocJSONResult format(String json) {
try {
return MAPPER.readValue(json, IMoocJSONResult.class);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
*
* @Description: Object is a collection transformation
* The object to be transformed is a list
* @param jsonData
* @param clazz
* @return
*
* @author leechenxiang
* @date 22 April 2016 8:35 PM: 31
*/
public static IMoocJSONResult formatToList(String jsonData, Class<?> clazz) {
try {
JsonNode jsonNode = MAPPER.readTree(jsonData);
JsonNode data = jsonNode.get("data");
Object obj = null;
if (data .isArray() && data.size() > 0) {
obj = MAPPER.readValue(data.traverse(),
MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
}
return build(jsonNode.get( "status").intValue(), jsonNode.get("msg").asText(),obj);
} catch (Exception e) {
return null;
}
}

public String getOk() {
return ok;
}

public void setOk(String ok) {
this.ok = ok;
}

}

 

Step 2: The package that introduces the exception uses the following class

Reference Code.

Step 3: In the controller

Writing a 1/0 returns an error page

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326371454&siteId=291194637