SpringMVC的@RestControllerAdvice注解的使⽤⼀ 视图
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;  charset=UTF-8">
<title>异常处理⽰例</title>
<script type="text/javascript"  src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(function(){
$("#search").click(function(){
$.post("${tPath}/search",null,
function(data){
// 处理异常
if (ssage)
{
alert("与服务器交互出现异常:" +  ssage);
}
else
{
// 获取服务器响应,显⽰所有订单信息
}
},"json");
});
springmvc的注解有哪些})
</script>
</head>
<body>
<button id="search">查询订单(返回JSON)</button>
</body>
</html>
⼆ 控制器及其处理类
1 OrderController
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class OrderController {
@PostMapping("/search")
public String search() throws Exception{
try {
int i = 5/0;
return "success";
} catch (Exception e) {
e.printStackTrace();
throw new OrderException("订单查询失败!");
}
}
}
2 OrderException
package ller;
public class OrderException extends RuntimeException {
public OrderException() {
super();
// TODO Auto-generated constructor stub
}
public OrderException(String message, Throwable cause,  boolean enableSuppression, boolean writableStackTrace) {          super(message, cause, enableSuppression,  writableStackTrace);
// TODO Auto-generated constructor stub
}
public OrderException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public OrderException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public OrderException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
3 GlobalExceptionHandler
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/
**
* @RestController注解本⾝使⽤@ControllerAdvicer和@ResponseBody注解。
* 使⽤了@RestControllerAdvice注解的类会被看作⼀个ControllerAdvicer,
* ⽽该类中所有使⽤@ExceptionHandler注解的⽅法都默认使⽤了的@ResponseBody注解。* */
@RestControllerAdvice
public class GlobalExceptionHandler {
// 处理OrderException⾃定义异常
@ExceptionHandler(value = OrderException.class)
public Object OrderErrorHandler(Exception e) throws Exception {
// 创建返回对象Map并设置属性,会被@ResponseBody注解转换为JSON返回
Map<String, Object> map = new HashMap<>();
map.put("code", 100);
map.put("message", e.getMessage());
map.put("data", "请求失败");
return map;
}
}
三 配置⽂件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:p="/schema/p"
xmlns:mvc="/schema/mvc"
xmlns:context="/schema/context"
xsi:schemaLocation="
/schema/beans
/schema/beans/spring-beans.xsd
/schema/mvc
/schema/mvc/spring-mvc.xsd
/schema/context
/schema/context/spring-context.xsd">
<!-- spring可以⾃动去扫描base-pack下⾯的包或者⼦包下⾯的java⽂件,如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean  -->
<context:component-scan base-package="ller"/>
<!-- 默认配置⽅案 -->
<mvc:annotation-driven/>
<!-- 静态资源处理 -->
<mvc:default-servlet-handler/>
<!-- 视图解析器  p:prefix属性表⽰前缀  p:suffix 表⽰后缀  -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/content/" p:suffix=".jsp"/>
</beans>
四 测试

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。