0

I am trying to upload files and json object in single request. Basically, we have requirement that when user uploads the certificate it should not be stored in database or in file system. It should be immediately processed and discarded.

I created the API that would accept multipart/form-data.

Tried several methods to parse JSON object as String with @RequestParam, @RequestBody and wrapper

My Controller look like below snippet

@RequestMapping(value = "v2/loadbalancers", method = RequestMethod.POST)
@ApiOperation(value = "Create load balancer", notes = "Create load balancer", produces = MediaType.APPLICATION_JSON_VALUE)
@Authorized(rolesAllowed = {Roles.ALL})
public ResponseEntity<ResponseObject<LoadBalancerResponse>> submitCreateVipRequest(HttpServletRequest request,
                                                                                       @RequestParam(value = "lbRequest", required = false) String lbRequest,
                                                                                       @RequestParam(value = "cert", required = false) MultipartFile certificate,
                                                                                       @RequestParam(value = "key", required = false) MultipartFile key){
        System.out.println("Debug line");
        return null;
}

In Spring Controller, I am able to see the files but, json or string content is null. If I set

@RequestParam(value = "lbRequest", required = true) String lbRequest

Header in postman console look like

POST /cloudgateway/network/v2/loadbalancers
Authorization: eyJhbGciOiJIUzUxMiJ9......omitted
Content-Type: multipart/form-data; boundary=--------------------------050647545435114222268928
cache-control: no-cache
Postman-Token: bb9e957e-05b5-46d1-ad02-0348cf051a5c
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Host: 10.70.227.127:8080
accept-encoding: gzip, deflate
content-length: 254686
lbRequest={"lbRequest": "value"}cert=[object Object]key=[object Object]
HTTP/1.1 200
status: 200
Node-Name: R72
X-Request-ID: e7cf3dfdb479af44617686f59e997f49:617686f59e997f49:0:0
Content-Length: 0
Date: Tue, 13 Aug 2019 23:55:05 GMT
Proxy-Connection: keep-alive
Connection: keep-alive

That throws missingservletrequestparameterexception - "Required String parameter 'lbRequest' is not present" I am using Postman form-data for testing and that has text type in key field.

My Problem is similar to MissingServletRequestParameterException

I tried several options like

Spring Controller @RequestBody with file upload is it possible?

Spring @RequestParam on multipart/form-data

randomkick
  • 47
  • 1
  • 11

0 Answers0