好吧,我正在使用名为 Thrive cart 的支付服务,我正在为我正在创建的成员(member)网站执行此操作。当用户付款后,我希望他们被重定向到一个 URL,我可以在其中使用该数据来更新当前用户信息。
在参数中发送的数据是疯狂的:
到目前为止,我已经设置了一个没有被击中的简单 Controller :
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping(value = "/welcome", method = RequestMethod.POST)
public void welcomeMember(@PathVariable String data) {
System.out.println(data);
}
}
我该如何处理这样疯狂的数据?我必须指定每个路径参数吗?
最佳答案
首先,您似乎获取的不是路径元素,而是请求参数,因此您需要 @RequestParam 注解来获取值。
由于请求参数太多,我还建议只采用一个参数,即 Map<String, String>
。该映射将包含所有参数作为键/值对,例如:
- 键:“thrivecart[account_id]”
- 值:“3196”
如果您不确定收到的是 POST 还是 GET 请求,您还可以添加第二个参数来接收 HttpMethod
.
将 RestController 更改为:
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping(value = "/welcome")
public void welcomeMember(@RequestParam Map<String, String> data, HttpMethod method) {
System.out.println(method);
System.out.println(data);
}
}
关于Java Spring Controller 处理一个荒谬的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53022110/