Skip to content

About accepting parameters for multipart/form-data requests in the Controller #34872

@457352727

Description

@457352727

While using the Controller to receive parameters for multipart/form-data requests, I found that the spring.servlet.multipart.resolve-lazily configuration option has an impact on whether the request parameters are assigned to object properties.


Here is an example:

Versions:

  • spring-boot: 3.4.5
  • spring-web: 6.2.5
  • spring-webmvc: 6.2.5

Code:

import jakarta.servlet.http.HttpServletResponse;
import java.io.Serial;
import java.io.Serializable;
import java.util.List;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demo")
public class DemoController {

  @PostMapping(value = "/bundle", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public void generateBundle(Person person, HttpServletResponse response) {
    // ...
  }

  public static class Person implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    private String name;
    private Integer age;
    private List<String> hobby;

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public Integer getAge() {
      return age;
    }

    public void setAge(Integer age) {
      this.age = age;
    }

    public List<String> getHobby() {
      return hobby;
    }

    public void setHobby(List<String> hobby) {
      this.hobby = hobby;
    }
  }
}

HTTP request:

POST /demo/bundle HTTP/1.1
Cookie: SESSION=NTVmY2MwOTctNGFhMS00YTcxLWJlYTgtZWIyOTFmYTU2ZWJm
User-Agent: PostmanRuntime/7.43.3
Accept: */*
Cache-Control: no-cache
Postman-Token: 7d9c80b3-df15-4691-a444-ee60b26444d3
Host: 127.0.0.1:8888
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------------------------716551840207761516003096
Content-Length: 485
 
----------------------------716551840207761516003096
Content-Disposition: form-data; name="name"
xiaoming
----------------------------716551840207761516003096
Content-Disposition: form-data; name="age"
20
----------------------------716551840207761516003096
Content-Disposition: form-data; name="hobby"
foo
----------------------------716551840207761516003096
Content-Disposition: form-data; name="hobby"
bar
----------------------------716551840207761516003096--

Description:

  • When spring.servlet.multipart.resolve-lazily is false

The properties in person are all null.

  • When spring.servlet.multipart.resolve-lazily is true

The properties in person can be assigned the correct value.


I don't know if this is a bug, and I'm not sure if there's something wrong with the way I'm using it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions