java - Spring 无法正确解码 form-urlencoded 值
问题描述
我已经阅读了这个答案,因为我的问题与问题相似,但现在我被卡住了。
通过邮递员,我以这种方式发送数据:
在春天我像这样检索它们:
@PostMapping(path = PathConstants.START_ACTION, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<BaseResponse<ProcessInstance>> start(@PathVariable String processDefinitionId,
@RequestParam("username") String params) {
这是它的工作,我可以打印用户名的值:
System.out.println("Username " + params);
问题是我需要发送的所有参数,但我无法将它们作为字符串或任何其他对象获取,因为我将发出许多不同的请求,而且并非所有请求都有“用户名”字段,并且顺便说一句,我需要收集所有这些。
我怎样才能做到这一点?我试过做
@PostMapping(path = PathConstants.START_ACTION, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<BaseResponse<ProcessInstance>> start(@PathVariable String processDefinitionId,
@RequestParam String params) {
或者
@PostMapping(path = PathConstants.START_ACTION, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<BaseResponse<ProcessInstance>> start(@PathVariable String processDefinitionId,
String params) {
正如其他答案中所建议的那样,但在这些情况下 params 为空。什么是正确的工作方式?
解决方案
您需要@RequestBody
方法中的注释,我建议使用MultiValueMap
:
@PostMapping(path = PathConstants.START_ACTION, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<BaseResponse<ProcessInstance>> start(@PathVariable String processDefinitionId, @RequestBody MultiValueMap<String, String> params)
推荐阅读
- python-3.x - 如何使用 Python 脚本为 Excel 工作表中的值形成一对多关系?
- r - 在 R 中进行混合方差分析时,如何获得残差并检查它们的正态性?
- css - 如何在悬停时更改 react.js 材料 ui 中嵌套列表项的 css 属性?
- ocaml - 在 64 位操作系统上安装内核
- node.js - npm run build 生成错误的资源 URL
- html - 我得到这个 werkzeug.exceptions.BadRequestKeyError
- python - 我们可以在列表中传递函数吗?
- javascript - jquery 的 .removeclass 删除类但不删除类属性
- maven - 了解 Maven 插件的目的
- c# - Unity Speech Recognition C# dictionary problem