1 测试类
package couragesteak.service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyIndexService {
@RequestMapping("/index")
public int MyIndexPage(int num) {
int j = 1 / num;
return j;
}
}
2 异常处理
2.1 返回json
package couragesteak.service;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@ControllerAdvice
public class CsExceptionHandler {
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public Map<Object, Object> exceptionHandler() {
HashMap<Object, Object> result = new HashMap<>();
result.put("code", 500);
result.put("msg", "系统错误");
return result;
}
}