一、概述

在为前端提供http接口时,通常返回的数据需要统一的json格式,如包含错误码和错误信息等字段。

该功能的实现有四种可能的方式:

    • AOP 利用环绕通知,对包含@RequestMapping注解的方法统一处理

      • 优点:配置简单、可捕获功能方法内部的异常
      • 缺点:aop不能修改返回结果的类型,因此功能方法的返回值须统一为Object类型
    • filter 在过滤器层统一处理
      • 优点:配置简单
      • 缺点:无法识别异常结果,须对返回结果进行额外的反序列化
    • 拦截器  获取返回值不方便,且无法获取到String类型的返回值,无法实现该功能
    • HandlerMethodReturnValueHandler 无上述各方法的缺点,且能复用@ResponseBody等注解,为该功能的完美实现方案

二、基于HandlerMethodReturnValueHandler的实现方案

HandlerMethodReturnValueHandler是spring mvc为统一处理控制器功能方法返回结果的接口类,为策略模式实现,视图名称的解析和@ResponseBody输出json等功能均为基于该接口的实现。spring mvc处理流程的源码分析可参考自定义统一api返回json格式

具体实现方案如下:

定义统一的返回实体

public class ResponseInfo {
public static final int ERROR_CODE_SUCCESS = 0;
public static final int ERROR_CODE_MAPPING_FAILED = 100;
public static final int ERROR_CODE_BUSINESS_FAILED = 130; /**
* 错误码
*/
private int errorCode; /**
* 错误信息
*/
private String errorMsg; /**
* 数据
*/
private Object data;
...
}

自定义HandlerMethodReturnValueHandler实现类

/**
* 对controller返回的数据统一封装为ResponseInfo,注意:
* 1、controller异常由spring mvc异常机制处理,会跳过该处理器
* 2、该处理器仅处理包含@RestController、@ResponseBody注解的控制器*/
public class MyHandlerMethodReturnValueHandler implements HandlerMethodReturnValueHandler { @Override
public boolean supportsReturnType(MethodParameter returnType) {
Class<?> controllerClass = returnType.getContainingClass();
returnType.getMethodAnnotation(ResponseBody.class); return controllerClass.isAnnotationPresent(RestController.class)
|| controllerClass.isAnnotationPresent(ResponseBody.class)
|| returnType.getMethodAnnotation(ResponseBody.class) != null;
} @Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest) throws Exception { ResponseInfo responseInfo = new ResponseInfo();
if (returnValue instanceof ResponseInfo) {
responseInfo = (ResponseInfo) returnValue;
}
else {
responseInfo.setData(returnValue);
} // 标识请求是否已经在该方法内完成处理
mavContainer.setRequestHandled(true); HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
response.setContentType("application/json;charset=UTF-8");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.getWriter().write(JSON.toJSONString(responseInfo));
}
}

实例化和注册该处理器

@Configuration
public class WebConfig implements ApplicationContextAware { /**
* 实例化为bean
*/
@Bean
public MyHandlerMethodReturnValueHandler myHandlerMethodReturnValueHandler() {
return new MyHandlerMethodReturnValueHandler();
} /*
* 注册到容器,采用这种注册方式的目的:
* 自定义的HandlerMethodReturnValueHandler放在默认实现的前面,从而优先采用自定义处理策略
* 否则,无法覆盖@ResponseBody处理机制,且String类型的返回值将默认由ViewNameMethodReturnValueHandler处理而映射为视图名
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { RequestMappingHandlerAdapter handlerAdapter = applicationContext.getBean(RequestMappingHandlerAdapter.class);
List<HandlerMethodReturnValueHandler> handlers = new ArrayList<HandlerMethodReturnValueHandler>();
handlers.add(this.myHandlerMethodReturnValueHandler());
handlers.addAll(handlerAdapter.getReturnValueHandlers());
handlerAdapter.setReturnValueHandlers(handlers);
}
}

spring-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:property-placeholder location="classpath:application.properties"/> <context:component-scan base-package="cn.matt" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="assignable" expression="cn.matt.common.web.WebConfig" />
</context:component-scan> </beans>

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="cn.matt" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController" />
<context:include-filter type="assignable" expression="cn.matt.common.web.WebConfig" />
</context:component-scan> </beans>

控制器基类

public class BaseController {

    @ExceptionHandler
public void exp(HttpServletRequest request, HttpServletResponse response, Exception ex) throws IOException {
response.setContentType("text/plain;charset=UTF-8");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0); ResponseInfo responseInfo = new ResponseInfo();
responseInfo.setErrorCode(ResponseInfo.ERROR_CODE_MAPPING_FAILED);
responseInfo.setErrorMsg(ex.getMessage());
response.getWriter().write(JSON.toJSONString(responseInfo));
}
}

测试控制器

@RestController
@RequestMapping("/test")
public class TestController extends BaseController { @RequestMapping(value = "/hello")
public String hello() {
return "hello";
} @RequestMapping(value = "/user")
public UserInfo getUser() {
UserInfo userInfo = new UserInfo();
userInfo.setUserName("matt");
userInfo.setProvince("安徽");
userInfo.setCity("阜阳");
return userInfo;
}
}

启动后,输入http://localhost:8080/wfc-web/test/user,http输出:

{"data":{"city":"阜阳","province":"安徽","userName":"matt"},"errorCode":0}

* 上述基于继承的异常处理方式,有一定侵入性,基于@RestControllerAdvice 或 @ControllerAdvice注解的方案无侵入性,详细可参考 Spring Boot 系列(八)@ControllerAdvice 拦截异常并统一处理

参考:

SpringMVC HandlerMethodReturnValueHandler解读

spring mvc 处理Controller返回结果和HandlerMethodReturnValueHandler使用

自定义统一api返回json格式(app后台框架搭建三)

springmvc获取上下文ApplicationContext

Spring MVC 使用介绍(十二)控制器返回结果统一处理的更多相关文章

  1. Spring MVC 框架结构介绍(二)

    Spring MVC框架结构 Spring MVC是围绕DispatcherServlet设计的,DispatcherServlet向处理程序分发各种请求.处理程序默认基于@Controller和@R ...

  2. Spring MVC 使用介绍(二)—— DispatcherServlet

    一.Hello World示例 1.引入依赖 <dependency> <groupId>javax.servlet</groupId> <artifactI ...

  3. Spring MVC 使用介绍(十五)数据验证 (二)依赖注入与方法级别验证

    一.概述 JSR-349 (Bean Validation 1.1)对数据验证进一步进行的规范,主要内容如下: 1.依赖注入验证 2.方法级别验证 二.依赖注入验证 spring提供BeanValid ...

  4. Spring MVC 使用介绍(十四)文件上传下载

    一.概述 文件上传时,http请求头Content-Type须为multipart/form-data,有两种实现方式: 1.基于FormData对象,该方式简单灵活 2.基于<form> ...

  5. Spring MVC体系结构和处理请求控制器

    Spring MVC体系结构和处理请求控制器 一:MVC设计模式: (1.)数据访问接口:DAO层 (2.)处理业务逻辑层:Service层 (3.)数据实体:POJO (4.)负责前段请求接受并处理 ...

  6. 【Spring MVC系列】--(4)返回JSON

    [Spring MVC系列]--(4)返回JSON 摘要:本文主要介绍如何在控制器中将数据生成JSON格式并返回 1.导入包 (1)spring mvc 3.0不需要任何其他配置,添加一个jackso ...

  7. Spring MVC 使用介绍(十三)数据验证 (一)基本介绍

    一.消息处理功能 Spring提供MessageSource接口用于提供消息处理功能: public interface MessageSource { String getMessage(Strin ...

  8. Spring Boot 2.X(十二):定时任务

    简介 定时任务是后端开发中常见的需求,主要应用场景有定期数据报表.定时消息通知.异步的后台业务逻辑处理.日志分析处理.垃圾数据清理.定时更新缓存等等. Spring Boot 集成了一整套的定时任务工 ...

  9. Spring MVC 4.1.4 RESTFUL风格返回JSON数据406错误处理

    Spring MVC 4.1.4 RESTFUL风格返回JSON数据406错误处理 今天在使用spring4.1.4,使用ResponseBody注解返回JSON格式的数据的时候遇到406错误. 解决 ...

  10. thinkPHP 模板中的语法知识 详细介绍(十二)

    原文:thinkPHP 模板中的语法知识 详细介绍(十二) 本章节:介绍模板中的语法,详细的语法介绍 一.导入CSS和JS文件    ==>记住常量的是大写 1.css link .js  sc ...

随机推荐

  1. 痞子衡嵌入式:恩智浦MCU安全加密启动一站式工具NXP-MCUBootUtility用户指南

    NXP MCU Boot Utility English | 中文 1 软件概览 1.1 介绍 NXP-MCUBootUtility是一个专为NXP MCU安全加密启动而设计的工具,其特性与NXP M ...

  2. 简述Servlet的基本概念

    Servlet的基本概念 Servlet的概念 http协议作用于客户端-服务端.由客户端发送请求(Request),服务器端接收到数据之后,向客户端发送响应(Response),这就是请求-响应模式 ...

  3. Java开发笔记(序)章节目录

    现将本博客的Java学习文章整理成以下笔记目录,方便查阅. 第一章 初识JavaJava开发笔记(一)第一个Java程序Java开发笔记(二)Java工程的帝国区划Java开发笔记(三)Java帝国的 ...

  4. DevOps概述

    Devops概念 转载自 devops实践-开篇感想 DevOps(英文Development和Operations的组合)是一组过程.方法与系统的统称,用于促进开发(应用程序/软件工程).技术运营和 ...

  5. (七) Keras 绘制网络结构和cpu,gpu切换

    视频学习来源 https://www.bilibili.com/video/av40787141?from=search&seid=17003307842787199553 笔记 首先安装py ...

  6. 网络爬虫BeautifulSoup库的使用

    使用BeautifulSoup库提取HTML页面信息 #!/usr/bin/python3 import requests from bs4 import BeautifulSoup url='htt ...

  7. 【English】七、常见动词

    一.动词: touch.hear.say.listen touch [tʌtʃ] 触摸 I touch the cat. They touch the elephant. hear  [hɪr] 听到 ...

  8. jsp页面中include静态html出现乱码问题的解决方式

    这个问题出现过两次,上次没有做好记录,今天又出现了.不过这两次的情景也不完全一致. 今天通过搜索找到这篇文章的解决方式很好,可供参考.原博客地址http://blog.csdn.net/izgnaw/ ...

  9. <自动化测试方案_8>第八章、手机端UI自动化测试

    第八章.手机端UI自动化测试 (一)APP测试分类 1,原生APP:Native页面是使用原生系统内核的,相当于直接在系统上操作 2,H5APP:先调用系统的浏览器内核,相当于是在网页中进行操作,较原 ...

  10. 从0开始的Python学习013编写一个Python脚本

    通过之前的学习我们已经了解了Python的很多基础运用了,现在我们尝试着做一个有使用价值的小脚本. 问题 需求: 我想要一个可以给我备份重要文件的程序. 需求分析: 首先文件是有存储路径,文件的路径和 ...