RequestBodyAdvice和注解方式进行统一参数处理demo
RequestBodyAdvice和注解方式进行统一参数处理demo
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface HttpBodyDecrypt { } import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.mytester.entity.Student;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice; import java.io.*;
import java.lang.reflect.Type;
import java.nio.charset.Charset; @ControllerAdvice(basePackages = "com.example.mytester.controller")
public class GlobalRequestBodyAdvice implements RequestBodyAdvice { @Override
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
System.out.println(">>>GlobalRequestBodyAdvice supports");
//只有打上注解标记的才执行 @HttpBodyDecrypt
boolean flag = methodParameter.hasMethodAnnotation(HttpBodyDecrypt.class);
if(flag) {
return true;
}
return false;
} @Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
System.out.println(">>>GlobalRequestBodyAdvice beforeBodyRead");
//方案2
return new XHttpInputMessage(inputMessage, "UTF-8"); //方案1
// StringBuilder sb = new StringBuilder();
// BufferedReader reader = null;
// try {
// reader = new BufferedReader(new InputStreamReader(inputMessage.getBody(), Charset.defaultCharset()));
// String line;
// while ((line = reader.readLine()) != null) {
// sb.append(line);
// }
// } catch (IOException e) {
// e.printStackTrace();
// throw new RuntimeException(e);
// } finally {
// if (reader != null) {
// try {
// reader.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
// JSONObject jsonObject = JSONObject.parseObject(sb.toString());
// System.out.println("覆盖之前的json串=" + jsonObject.toString());
//
// if (jsonObject != null){
// //改变
// if(jsonObject.get("name") != null) {
// jsonObject.put("name", "AMD");
// }
//
// //针对字段来处理
// String afterStr = jsonObject.toJSONString();
// System.out.println("覆盖之后的json串="+afterStr);
//
// //字符串转输入流
// InputStream rawInputStream = new ByteArrayInputStream(afterStr.getBytes());
// return new HttpInputMessage(){
//
// @Override
// public HttpHeaders getHeaders() {
// return inputMessage.getHeaders();
// }
//
// @Override
// public InputStream getBody() throws IOException {
// return rawInputStream;
// }
// };
// }
// return inputMessage;
} @Override
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
System.out.println(">>>GlobalRequestBodyAdvice afterBodyRead");
return body;
} @Override
public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
System.out.println(">>>GlobalRequestBodyAdvice handleEmptyBody");
return body;
}
} import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.mytester.entity.Student;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage; import java.io.*;
import java.nio.charset.Charset; public class XHttpInputMessage implements HttpInputMessage {
private HttpHeaders headers;
private InputStream body; public XHttpInputMessage(HttpInputMessage httpInputMessage, String encode) throws IOException {
this.headers = httpInputMessage.getHeaders();
this.body = encode(httpInputMessage,httpInputMessage.getBody(), encode);
} private InputStream encode(HttpInputMessage httpInputMessage, InputStream body, String encode) {
//省略对流进行编码的操作
StringBuilder sb = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(body, Charset.defaultCharset()));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
JSONObject jsonObject = JSONObject.parseObject(sb.toString()); if (jsonObject != null){
System.out.println("XHttpInputMessage获取json=" + jsonObject.toString()); Student student = JSONObject.parseObject(jsonObject.toString(), Student.class); System.out.println("XHttpInputMessage修改之前的学生名称为:" + student.getName());
student.setName("AMD"); //改变值 String afterStr = JSON.toJSONString(student); //字符串转输入流
InputStream rawInputStream = new ByteArrayInputStream(afterStr.getBytes());
return rawInputStream;
} return body;
} @Override
public InputStream getBody() throws IOException {
return body;
} @Override
public HttpHeaders getHeaders() {
return headers;
}
} public class ClassRoom {
private static final long serialVersionUID = -339516038496531943L;
private String sno;
private String name;
private String sex;
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
} @Override
public String toString() {
return "Student{" +
"sno='" + sno + '\'' +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
} @RestController
public class TestController { /**
* http://localhost:8080/addstudent4
* 参数:{"sno":"11124","name":"xiaoming","sex":"man"}
* 返回:
* Student{sno='11124', name='AMD', sex='man'}
*
* @param student
* @param request
* @return
*/
@HttpBodyDecrypt
@RequestMapping(value = "/addstudent4", method = RequestMethod.POST)
public String saveStudent4(@RequestBody ClassRoom student, HttpServletRequest request) {
return student.toString();
}
}
RequestBodyAdvice和注解方式进行统一参数处理demo的更多相关文章
- SpringMVC的controller方法中注解方式传List参数使用@RequestBody
在SpringMVC控制器方法中使用注解方式传List类型的参数时,要使用@RequestBody注解而不是@RequestParam注解: //创建文件夹 @RequestMapping(value ...
- Java 注解方式校验请求参数
1. 参数校验常用注解 注解 验证的数据类型 备注 Null 任意类型 参数值必须是 Null NotNull ...
- spirngboot 注解方式注入自定义参数
在代码中 @value("oracle.user") private String user; 在配置文件中 oracle.user=root
- 【SpringAop】【统一日志处理】注解方式理解以及使用
[注意:本次代码的demo会存在百度网盘,由于公司的保密,禁止上传,所以仅本人可见] 目前公司在做数据资产项目,数据质量部分使用到了springaop做统一日志处理,以前对这块有了解,有点模糊不清,今 ...
- 【spring mvc】spring mvc POST方式接收单个字符串参数,不加注解,接收到的值为null,加上@RequestBody,接收到{"uid":"品牌分类大”},加上@RequestParam报错 ---- GET方式接收单个参数的方法
spring mvc POST方式 接收单个参数,不加任何注解,参数名对应,接收到的值为null spring mvc POST方式 接收单个参数,加上@RequestBody,接收到参数格式:{&q ...
- spring 纯注解方式 与AOP
spring注解方式 以前我也使用过纯注解方式.现在在这里做个记录 我们先认识几个我们都耳熟能详的注解 @configuration :从spring3.0这个注解就可以用于定义配置类,可以替换xml ...
- 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法
spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...
- spring----IOC注解方式以及AOP
技术分析之Spring框架的IOC功能之注解的方式 Spring框架的IOC之注解方式的快速入门 1. 步骤一:导入注解开发所有需要的jar包 * 引入IOC容器必须的6个jar包 * 多引入一个:S ...
- Spring中的AOP注解方式和XML方式
应掌握内容:1. AOP的全名2. AOP的实现原理[静态代理和动态代理]3. 注解方式的配置4. 通知类型 A. 每种通知的特点和使用方式 B. 获取各种数据,方便日后操作5. 执行表 ...
- Spring系列之aAOP AOP是什么?+xml方式实现aop+注解方式实现aop
Spring系列之aop aop是什么?+xml方式实现aop+注解方式实现aop 什么是AOP? AOP为Aspect Oriented Programming 的缩写,意识为面向切面的编程,是通过 ...
随机推荐
- 开源 1 年半 star 破 1.2 万的 Dapr 是如何在阿里落地的?
简介: Dapr 是 2019 年 10 月微软开源的可移植.事件驱动分布式运行时,它使开发人员能够轻松地构建运行在云平台和边缘的弹性而微服务化的无状态和有状态的应用程序,从而降低基于微服务架构构建现 ...
- dotnet 如何将 Microsoft.Maui.Graphics 对接到 UNO 框架
本文将和大家介绍如何将 Microsoft.Maui.Graphics 对接到 UNO 框架里面.一旦完成 Microsoft.Maui.Graphics 对接,即可让 UNO 框架复用现有的许多绘制 ...
- RT-Thead移植时缺少SystemCoreClockUpdate函数
一.问题 在移植 RT-Thead 时,需要获取mcu当前的 HCLK 时钟频率来设置滴答时钟,而我使用的库函数中刚好缺少SystemCoreClockUpdate函数. 我之前在RT-Thread移 ...
- 习题8 #第8章 Verilog有限状态机设计-1 #Verilog #Quartus #modelsim
1. 设计一个"111"串行数据检测器.要求是:当检测到连续3个或3个以上的"1"时输出为1,其他输入情况下输出为0. (1)思路分析:参照本章前文的范例,如第 ...
- 一个随时更新的js库
1.src同级建commFunction=>timer.js 2.main.js引入 import time from '../commonFunction/time' Vue.prototyp ...
- Solution - AGC060C
Link 简要题意:称一个长为 \(2^n-1\) 的排列 \(P\) 像堆,如果 \(P_i \lt P_{2i}\),且 \(P_i \lt P_{2i+1}\).给定 \(a,b\),设 \(u ...
- ansible(9)--ansible的yum模块
1. yum模块 功能:管理软件包,需要确认被管理端为红帽系列的,并且需要被管理端配置好yum源. 主要的参数如下: 参数 说明 name 指定安装软件包名或软件包URL state 指定yum对应的 ...
- XML Schema(XSD)详解:定义 XML 文档结构合法性的完整指南
XML Schema描述了 XML 文档的结构.XML Schema语言也称为 XML Schema Definition(XSD). <?xml version="1.0" ...
- 九、.net core(.NET 6)添加通用的Redis功能
.net core 编写通用的Redis功能 在 Package项目里面,添加包:StackExchange.Redis: 在Common工具文件夹下,新建 Wsk.Core.Redis类库项目,并 ...
- 西门子PLC设备如何接入AIRIOT物联网低代码平台 ?
西门子PLC设备广泛应用于工业控制领域,高性能和稳定是它最大的优势.下面我们要把西门子300 1200 1500 PLC设备连接到AIRIOT物联网低代码平台,具体操作如下所示: 西门子驱动配置(配套 ...