Spring 启动@RequestPart 字符集编码

Spring Boot @RequestPart charset encoding

我有一个端点 consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE 使用 POST 方法。

有 2 个参数 @RequestPart(name = "model", required = true) String jsonPart, @RequestPart(name = "images", required = false) MultipartFile[] images。第一个参数是一个 JSON 字符串,稍后我将使用 ObjectMapper 将其解析为 POJO,这很顺利,也接收 Multipart[]。唯一的问题是初始 JSON 字符串中的 name 值编码不正确。

这是来自 multipart-form/data requestJSON 字符串,已正确解析,这里我给它的值是 White Lé FrÖÖnt

这是 @RequestPart 中调试的 JSON 字符串,给出 White LeÌ FrÃnt,这不是预期的

以下是我迄今为止通过浏览网页收集的建议

  1. CharacterEncodingFilter 添加到 Spring 安全 FilterChain 尽可能优先
  2. server.servlet.encoding.enabled=trueserver.servlet.encoding.force=true 甚至 application.properties
  3. 中的 server.servlet.encoding.force=false

None 其中单独或共同工作。我的想法是 request 没有任何问题,而是我需要添加到服务器才能使其正常工作。感谢您的宝贵时间,感谢您的帮助。

解决该问题的一种方法是将参数类型设置为 MultipartFile

@RequestPart(name = "model", required = true) MultipartFile model 

然后使用 model.getBytes() 获取内容并将其传递给 ObjectMapper