- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 Spring 的 @RequestBody
注释和 MappingJacksonHttpMessageConverter
将请求的 JSON POST 数据映射到一个对象中。但是在那之后我想以 String
形式读取数据以进行一些额外的身份验证。但是当编码发生时,HttpServletRequest
中的 InputStream
是空的。一旦我从方法中删除了 @RequestBody
参数,将 POST 数据读取到 String
中就会按预期工作。
我是否必须通过放弃 @RequestBody
并以某种方式手动进行绑定(bind)来妥协,或者是否有更优雅的解决方案?
最佳答案
因此,基本上您需要计算请求正文的哈希值。优雅的方法是对 InputStream
应用装饰器。
例如,在处理程序方法内部(在这种情况下您不能使用 @RequestBody
并且需要手动创建 HttpMessageConverter
):
@RequestMapping(...)
public void handle(HttpServletRequest request) throws IOException {
final HashingInputStreamDecorator d =
new HashingInputStreamDecorator(request.getInputStream(), secretKey);
HttpServletRequest wrapper = new HttpServletRequestWrapper(request) {
@Override
public ServletInputStream getInputStream() throws IOException {
return d;
}
};
HttpMessageConverter conv = ...;
Foo requestBody = (Foo) conv.read(Foo.class, new ServletServerHttpRequest(wrapper));
String hash = d.getHash();
...
}
其中哈希是在 HashingInputStreamDecorator
的重写 read
方法中递增计算的。
如果您创建一个 Filter
来应用装饰器,您也可以使用 @RequestBody
。在这种情况下,装饰器可以将计算出的散列作为请求属性传递给处理程序方法。但是,您需要仔细映射此过滤器,以仅将其应用于对特定处理程序方法的请求。
关于json - 使用 Spring 的 @RequestBody 并在之后读取 HttpServletRequest.getInputStream(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4097012/
当我定义 requestBody 时,它不会显示在 swagger 文档中。我想为 swagger 中的 gpx 文件创建一组图像和单个文件上传。如何才能实现 requestBody 像参数属性一样显
我正在使用 Spring 4.x 做 Spring Rest Api 项目 这有效: Controller.java @PostMapping("newTransaction") Transactio
我正在向服务器发送一个 JSON 对象,该服务器将填充我的域对象报告。 public class CustomReport {String name; String name; String emai
我有一个非常琐碎的问题,它占用了我很多时间。 我有一个 Spring Rest 服务,它接受 @RequestBody 中的模型对象。我在模型对象中传递的是格式为 yyyy-MM-dd'T'HH:mm
我一直在开发 Spring Boot Rest api 服务,这是我的 Rest Controller 方法之一: @RequestMapping(value = "manager", method
我使用 Spring Boot 2.0.8.RELEASE。 我有一个 @RestController 方法,如下所示: @PostMapping("/message") public
类(class): class Person { private String name; private List addr; } c
我有以下类作为我的@RequestBody,但我无法发送我的请求,我尝试以不同的格式准备 JSON,但它们都不起作用。 处理程序 @RequestMapping("/grades") @Respons
@PatchMapping("/update") HttpEntity updateOnlyIfFieldIsPresent(@RequestBody Person person) { if(
我正在尝试使用 apache 集合中的 MultiValueMap(MultiMap 的实现)。我正在使用 Spring MVC 的 @RequestBody 注释。但是,我不断收到 HTTPMedi
Requestbody 没有映射到我在这里使用的对象。 PaymentRequest 中的所有字段都为空。我看到注释和映射一切似乎都是正确的。 @RestController @RequestMapp
假设以下 JSON: { "attr_A": "val_A", "array_A": [{ "attr_B": "val_B" }] } 以及以下两个类: public class C
JavaScript 代码: function deleteMarks(list){ $http.post('/api/marks/delete/all',list).then( fu
我是 Java/Spring 的新手,正在尝试在现有项目中设置 API 端点。我基本上已经复制了一些当前正在工作的其他端点,但我的端点在被击中时没有验证,这似乎是因为 @RequestBody 没有填
我正在使用 postman 来测试我的请求。所以我从 postman 那里选择了显示代码 fragment 的选项并选择了 OkHttp fragment 是: OkHttpClient client
我使用 Spring-boot 2.0.1 和 WebFlux 作为 Rest 服务器。 在我的 RestController 中,我想自动反序列化一个对象(产品)。但我收到 Jackson 错误,就
我创建了一个简单的 REST 服务 (POST)。但是当我从 postman 那里调用这个服务时,@RequestBody 没有收到任何值。 import org.springframework.ht
PostMan传参给@RequestBody(接受前端参数) 今天新接手一个项目框架,需要改造,但后台写好方法,准备用postman 测试时候,发现用以前传参方式不行,需要需要将json字符串转成
springmvc @RequestBody String类型参数 通过如下配置: <bean id="mappingJacksonHttpMessageConverter&qu
目录 SpringMVC @RequestBody为null 关于inputsteam的一些理解 @RequestBody 自动映射原理的简单介
我是一名优秀的程序员,十分优秀!