How to use @RequestBody and @RequestParam together

余生颓废 提交于 2020-05-16 08:57:29

问题


I'm trying to use both @RequestBody and @RequestParam to send JSON and multiple files through Postman but it's not working. Is it possible to use both annotations in an API?

@RequestMapping(value = "/save/product/test", method = RequestMethod.POST)
public ResponseEntity<?> save(@Valid @RequestBody ProductVo productVo, @RequestParam("files") @NotNull @NotBlank MultipartFile[] uploadfiles) {

    System.out.println("body " + productVo.toString());
    for (MultipartFile file :  uploadfiles) {
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getContentType());
        System.out.println(file.getName());
        System.out.println(file.getSize());

    }
    return new ResponseEntity<APIResponse>(this.apiResponse, HttpStatus.NO_CONTENT);
}

回答1:


You can ease your life by wrapping the multipart alongside the other fields:

  class UploadContext {

    private MultipartFile file;
    private UserCreateRequest request;
    // other fields
 }

and use this object in the controller:

    @PostMapping(value = "/upload")
    public void upload(UploadContext context) {
        // controller logic here
    }

Doc: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-multipart-forms

In your client code you have to clear the content-type from the headers:

     headers: {'Content-Type': undefined}

Hope this helps.




回答2:


@RequestParam takes parameter from uri, you are actually trying to achieve something else.

Here is an example controller takes json body and multipart file :

@RestController
@RequestMapping("/users")
public class UserController {

    UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping({"/", ""})
    public ResponseEntity<User> post(@RequestPart("request") UserCreateRequest request, @RequestPart("file") MultipartFile file) throws IOException {

        String photoPath = UUID.randomUUID() + file.getOriginalFilename().replaceAll(" ", "").trim();

        // other logic

        return ResponseEntity.ok(userService.create(request));
    }
}



回答3:


Any number of files and a string param can be uploaded by having a MultipartHttpServletRequest and RequestParam.

One thing to be aware of: The MultipartHttpServletRequest will also hold all the request params, so technically you can even just have MultipartHttpServletRequest and parse it

Signature of Controller is:

public ResponseEntity<BulkUploadResponsePayload> filesAndJson(
            @ApiParam(hidden = true) MultipartHttpServletRequest multipartRequest,
            @RequestParam(value = "json-param",name = "json-param") String documentType) {
     // multipartRequest will have all the files
     // you can use json-param for any string
}


来源:https://stackoverflow.com/questions/51917110/how-to-use-requestbody-and-requestparam-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!