request参数获取,参数校验,参数处理
需求:
1.post接口,需要在过滤器中进行参数校验,校验通过之后再执行方法
2.原有代码中使用x-www-form-urlencoded传参,新需求要使用json格式
3.原有代码校验过滤器使用ServletRequest.getParameter来获取参数,并将其放入ThreadLocal<OpenapiRequest>常量中进行校验
问题:
1.改用json传参之后,再过滤器中无法通过ServletRequest.getParameter来获取参数,所有参数为null,因此无法通过参数加密校验
'使用流读取参数,可以获取参数'
BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null) {
responseStrBuilder.append(inputStr);
}
String paramString = responseStrBuilder.toString();
2.参数校验通过,但是方法接收参数出错
Caused by: org.glassfish.hk2.api.MultiException: A MultiException has 7 exceptions. They are:
1. java.lang.IllegalStateException: The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded
2. java.lang.IllegalStateException: The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded
3. java.lang.IllegalStateException: The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded
4. java.lang.IllegalStateException: The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded
5. java.lang.IllegalStateException: The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded
6. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.seari.ztxplatform.openapi.model.OpenapiRequest errors were found
7. java.lang.IllegalStateException: Unable to perform operation: resolve on com.seari.ztxplatform.openapi.model.OpenapiRequest
解决方法:替换原方法的入参注解@BeanParam为@RequestBody
3.入参中普通String字段接收成功,其中一个参数data在类中定义为String,但是传参需要一个json对象,导致报错
本次响应数据:"Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@c507164;
line: 5, column: 13] (through reference chain: com.seari.ztxplatform.openapi.model.OpenapiRequest[\"data\"])"
此时可以在传参时,将data参数按照字符串格式传,而不是json格式,可以正常调用接口。样式如

4.前端需要统一入参格式,不能单独将data以String类型传参,要求的传参格式为

解决方法增加一个反序列化工具
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import groovy.util.logging.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils; import java.io.IOException; /**
* 用来自定义openapiRequest中的data在反序列化时的类型
*
* @author liming
* @since 2021/12/30 17:07
*/
@Component
@Slf4j
public class DataJsonDeserializer extends JsonDeserializer {
private final Logger log = LoggerFactory.getLogger(ApiParameterFilter.class); @Override
public String deserialize(JsonParser data, DeserializationContext ctxt)
throws IOException, JsonProcessingException { if (ObjectUtils.isEmpty(data)) {
return null;
}
String openapiData = data.toString(); log.info((" ====> "
+ data.getText() + ",转换后的结果 ====> " + openapiData)); return openapiData;
}
}
然后再接收参数类的data字段的set方法上加上注解
@JsonDeserialize(using = DataJsonDeserializer.class)
public void setData(String data) {
this.data = data;
}
从stackoverflow看到一个类型的问题,当时答题人提到关于这个问题可以去看看jsonDeserializer相关内容,最后试了下确实可以。真的是一句话拯救了我一天的时间,感谢!
For deserializing a node that can be either a String or an Object, you could give a look to @JsonSerialize giving a custom JsonDeserializer
详见
https://stackoverflow.com/questions/54062469/cannot-deserialize-instance-of-java-lang-string-out-of-start-object-token
request参数获取,参数校验,参数处理的更多相关文章
- 过滤器中处理multipart/form-data头部的post请求request.getParameter(")获取不到参数问题
如果不是文件类型请求,我们使用request.getParameter("");方法是可以获取到参数内容的,如果是文件类型的请求即请求的头部信息为“multipart/form-d ...
- 前端请求参数MD5加密校验,参数串解密
首先引入MD5加密库:=>https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js; 步骤:=>1.请求前对参数进行字典升序排序,排 ...
- SpringBoot08 请求方式、参数获取注解、参数验证、前后台属性名不一致问题、自定义参数验证注解、BeanUtils的使用
1 请求方式 在定义一个Rest接口时通常会利用GET.POST.PUT.DELETE来实现数据的增删改查:这几种方式有的需要传递参数,后台开发人员必须对接收到的参数进行参数验证来确保程序的健壮性 1 ...
- 深度学习原理与框架-Alexnet(迁移学习代码) 1.sys.argv[1:](控制台输入的参数获取第二个参数开始) 2.tf.split(对数据进行切分操作) 3.tf.concat(对数据进行合并操作) 4.tf.variable_scope(指定w的使用范围) 5.tf.get_variable(构造和获得参数) 6.np.load(加载.npy文件)
1. sys.argv[1:] # 在控制台进行参数的输入时,只使用第二个参数以后的数据 参数说明:控制台的输入:python test.py what, 使用sys.argv[1:],那么将获得w ...
- request.getParameter()获取URL中文参数乱码的解决办法
这个问题耽误好长时间,URL传中文参数出现乱码,就算首次使用request接收就添加 request.setCharacterEncoding("UTf-8"); 依然报错不误. ...
- jquery通过ajax-json访问java后台传递参数,通过request.getParameter获取不到参数的说明
http://m.blog.csdn.net/blog/eyebrother/36007145 所以当后台通过request.getParameter("name");对参数值的作 ...
- JavaScript根据参数获取url中参数名的值
//假设ulr如下var localhost="http://127.0.0.1?name=tom&sex=男&id=1";//正则方法封装function Get ...
- Http协议入门、响应与请求行、HttpServletRequest对象的使用、请求参数获取和编码问题
1 课程回顾 web入门 1)web服务软件作用: 把本地资源共享给外部访问 2)tomcat服务器基本操作 : 启动: %tomcat%/bin/startup.bat 关闭: %tomcat%/ ...
- Linux - Shell - 参数获取
概述 参数 背景 复习一下 shell 脚本的参数获取 场景 os centos7 1. 参数: 基础 概述 简单描述 参数 1. 获取参数 获取 第一个 参数 获取参数 使用 ${num} 获取参数 ...
- request param 获取
通过request对象获取客户端请求信息 getRequestURL方法返回客户端发出请求时的完整URL. getRequestURI方法返回请求行中的资源名部分. getQueryString 方法 ...
随机推荐
- 并发 并行 进程 线程 协程 异步I/O python async
一些草率不精确的观点: 并发: 一起发生,occurence: sth that happens. 并行: 同时处理. parallel lines: 平行线.thread.join()之前是啥?落霞 ...
- 【Linux】【Services】【SaaS】 kubeadm安装kubernetes
1. 简介 2. 环境 2.1. OS: CentOS Linux release 7.5.1804 (Core) 2.2. Ansible: 2.6.2-1.el7 2.3. docker: 2. ...
- Output of C++ Program | Set 1
Predict the output of below C++ programs. Question 1 1 // Assume that integers take 4 bytes. 2 #incl ...
- 【编程思想】【设计模式】【结构模式Structural】front_controller
Python版 https://github.com/faif/python-patterns/blob/master/structural/front_controller.py #!/usr/bi ...
- 【C/C++】拔河比赛/分组/招商银行
题目:小Z组织训练营同学进行一次拔河比赛,要从n(2≤n≤60,000)个同学中选出两组同学参加(两组人数可能不同).对每组同学而言,如果人数超过1人,那么要求该组内的任意两个同学的体重之差的绝对值不 ...
- iOS 实现简单的界面切换
以下是在iOS中最简单的界面切换示例.使用了多个Controller,并演示Controller之间在切换界面时的代码处理. 实现的应用界面: 首先,创建一个window-based applicat ...
- Windows查看端口被占用的程序!
"到底是谁占用了80端口,该怎么查,怎么终止它?",这里就简单讲解一下,在windows下如何查看80端口占用情况?是被哪个进程占用?如何终止等. 这里主要是用到windows下的 ...
- pwnable_start
第一次接触这种类型的题,例行检查一下 题目是32位 没有开启nx保护可以通过shellocode来获得shell 将题目让如ida中 由于第一次碰到这种题,所以我会介绍的详细一点, 可以看到程序中调用 ...
- 用Word做表,总会多出一页来?(Word技巧集团)
用Word做表的同学都有这样的体验,表格后面都会多出一行,默认的,去都去不掉.如果一个表刚好做到一页的最下面,嗯,那就多出一页来,如下图: 这一页,多不多余啊,讨不讨厌啊,可不可恶啊--可是Word同 ...
- LuoguP1723 高手过愚人节 题解
Content 有 \(n\) 次询问,每次询问给定一个字符串 \(s\),求这个字符串最长的回文子串的长度. 数据范围:\(n\) 无解(至少从题面来看是这样的),字符串长度目测应该在 \(10^7 ...