如何在同一请求中传递主体@RequestBody和@RequestParam [英] How to pass body @RequestBody and @RequestParam in same request

查看:450
本文介绍了如何在同一请求中传递主体@RequestBody和@RequestParam的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用
包在同一请求中传递正文和参数 dio

我在Spring Boot上具有以下方法:

I have this method on Spring boot:

@PostMapping("/guardarproducto")
    public ResponseEntity<Usuario> insertProduct(@RequestBody String body, @RequestParam("imagen") MultipartFile imagen) {
 ....
}

我尝试执行以下请求:

FormData formData = new FormData.from({
        "barcode": barcode != null ? this.barcode : null,
        "idUsuario": user.id,
        "nombre": _textController.text,
        "aditivos": aditivosLeidos,
        "imagen": pickedImage
      });
      await dio.post('https://10.0.2.2:8443/api/guardarproducto',
          data: formData);

但是我得到:

{
    "timestamp": "2019-07-03T12:11:39.902+0000",
    "status": 400,
    "error": "Bad Request",
    "message": "Required request body is missing: public org.springframework.http.ResponseEntity<ual.dra.rest.Usuario> ual.dra.rest.AditivoController.insertProduct(java.lang.String,org.springframework.web.multipart.MultipartFile)",
    "path": "/api/guardarproducto"
}

如何在同一请求中传递正文并请求参数?

How can i pass body and request param in the same request?

我不在乎使用Dio或Http包。

I don't care use Dio or Http package.

推荐答案

好的,我不确定抖动和飞镖的工作原理,但是在春季启动时,当您尝试同时发布两个 RequestBody MultiPart 考虑使用 @RequestPart

Okay I am not sure how flutter and dart works, but in spring boot when you try to post both RequestBody and MultiPart consider using @RequestPart

@PostMapping("/guardarproducto")
public ResponseEntity<Usuario> insertProduct(@RequestPart("body") String body,
                                             @RequestPart("imagen") MultipartFile imagen) {
 ....
}

,同时在 FormData 中发布正文时,您已经在一个对象中设置了json。让我们说以下是json。

and also while posting a body in FormData, you have set the json in one object.Let say below is the json.

{
        "barcode": "XAWA"
        "idUsuario": 1,
        "nombre": 1,
        "aditivos": "1"
}

然后在 FormData

FormData formData = new FormData.from({
        "body" : {  "barcode": barcode != null ? this.barcode : null,
                   "idUsuario": user.id,
                  "nombre": _textController.text,
                  "aditivos": aditivosLeidos
                 } 
        "imagen": pickedImage
      });

Note : "body" and "imagen" are in same level.

这篇关于如何在同一请求中传递主体@RequestBody和@RequestParam的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆