A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

【郑州校区】学成在线 第3天 讲义-CMS页面管理开发九

5.4 不可预知异常处理
5.4.1 定义异常捕获方法
5.4.1.1 异常抛出测试
使用postman测试添加页面,不输入cmsPost信息,提交,报错信息如下:
[AppleScript] 纯文本查看 复制代码
 org
.springframework.http
.converter.HttpMessageNotReadableException
此异常是springMVC在进行参数转换时报的错误。

具体的响应的信息为:
[AppleScript] 纯文本查看 复制代码
 {
"
timestamp
"
: 1528712906727,
"
status
"
: 400,
"
error
"
:
"
Bad Request
"
,
"
exception
"
:
"
org
.springframework.http
.converter.HttpMessageNotReadableException
"
,
"
message
"
:
"
Required request body is missing: public
com.xuecheng
.framework.domain.cms.response.CmsPageResult
com.xuecheng
.manage_cms.web.controller.CmsPageController.add(com.xuecheng
.framework.domain.cms.C
msPage)
"
,
"
path
"
:
"
/cms/page/add
"
}


上边的响应信息在客户端是无法解析的。
在异常捕获类中添加对Exception异常的捕获:
[AppleScript] 纯文本查看 复制代码
@ExceptionHandler(Exception.class)
@ResponseBody
public ResponseResult exception(Exception exception){
//记录日志
LOGGER.error(
"
catch exception:{}
"
,exception.
getMessage());
return null;
}


5.4.1.2 异常捕获方法
针对上边的问题其解决方案是:
1、我们在map中配置HttpMessageNotReadableException和错误代码。
2、在异常捕获类中对Exception异常进行捕获,并从map中获取异常类型对应的错误代码,如果存在错误代码则返回此错误,否则统返回99999错误。
具体的开发实现如下:
1、在通用错误代码类CommCode中配置非法参数异常

[AppleScript] 纯文本查看 复制代码
INVALID_PARAM(false,10003,
"
非法参数!
"
), 

2、在异常捕获类中配置 HttpMessageNotReadableException 为非法参数异常。
异常捕获类代码如下:
[AppleScript] 纯文本查看 复制代码
 package com.xuecheng
.framework.exception;
import com.
google.common.collect.ImmutableMap
import com.xuecheng
.framework.model.response.CommonCode;
import com.xuecheng
.framework.model.response.ResponseResult;
import com.xuecheng
.framework.model.response.ResultCode;
import org
.slf4j
.Logger;
import org
.slf4j
.LoggerFactory;
import org
.springframework.http
.converter.HttpMessageNotReadableException;
import org
.springframework.web.bind.annotation.ControllerAdvice;
import org
.springframework.web.bind.annotation.ExceptionHandler;
import org
.springframework.web.bind.annotation.ResponseBody;
/
**
*
@author Administrator
*
@version 1.0
*
@create 2018
‐
06
‐
11 17:16
**
/
@ControllerAdvice
public class ExceptionCatch {
private static final Logger LOGGER = LoggerFactory
.
getLogger(ExceptionCatch.class);
//使用EXCEPTIONS存放异常类型和错误代码的映射,ImmutableMap的特点的
一
旦创建不可改变,并且线程安全
private static ImmutableMap<Class<? extends Throwable>,ResultCode> EXCEPTIONS;
//使用builder来构建
一
个异常类型和错误代码的异常
protected static ImmutableMap
.Builder<Class<? extends Throwable>,ResultCode> builder =
ImmutableMap
.builder();
//捕获Exception异常
@ResponseBody
@ExceptionHandler(Exception.class)
public ResponseResult exception(Exception e) {
LOGGER.error(
"
catch exception : {}\r\nexception:
"
,e.
getMessage(), e);
if(EXCEPTIONS
== null)
EXCEPTIONS
=
builder.build();
final ResultCode resultCode
= EXCEPTIONS.
get(e.
getClass());
final ResponseResult responseResult;
if (resultCode !
= null) {
responseResult
= new ResponseResult(resultCode);
} else {
responseResult
= new ResponseResult(CommonCode.SERVER_ERROR);
}
return responseResult;
}
//捕获 CustomException异常
@ExceptionHandler(CustomException.class)
@ResponseBody
public ResponseResult customException(CustomException e) {
LOGGER.error(
"
catch exception : {}\r\nexception:
"
,e.
getMessage(), e);
ResultCode resultCode
=
e.
getResultCode();
ResponseResult responseResult
= new ResponseResult(resultCode);
return responseResult;
}
static{
//在这里加入
一
些基础的异常类型判断
builder.
put(HttpMessageNotReadableException.class,CommonCode.INVALIDPARAM);
}
}


5.4.3 异常处理测试
仍然模拟

问题测试

中的测试步骤,异常结果为

非法参数


6 实战
此部分为自学内容,根据今天所学知识完成下边的任务。
6.1 查询条件完善
页面查询条件增加:页面名称、页面类型。
页面名称对应CmsPage模型类中的pageName属性。
页面类型对应CmsPage模型类中的pageType属性。
查询要求:
页面名称:模糊查询
页面类型:精确匹配,页面类型包括:静态和动态,在数据库中静态用0表示,动态用1表示。
6.2 页面属性增加DataUrl
CmsPage.
java模型类型中有dataUrl属性,此属性在页面静态化时需要填写。
本需求要求:
1、在新增页面增加dataUrl输入框,并支持添加。
2、在修改页面增加dataUrl输入框,并支持修改。


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马