gpt4 book ai didi

java - Spring 无法正确解码表单 urlencoded 值

转载 作者:行者123 更新时间:2023-12-02 09:59:39 39 4
gpt4 key购买 nike

我已阅读 this answer因为我的问题与问题类似,但现在我陷入困境。
通过 postman 我以这种方式发送数据:

enter image description here

在 Spring 中我像这样检索它们:

@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)

关于java - Spring 无法正确解码表单 urlencoded 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55730201/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com