spring可以通过@RequestBody以外的方式映射POST参数吗
我在一个应用程序中使用spring可以通过@RequestBody以外的方式映射POST参数吗,spring,spring-mvc,spring-restcontroller,Spring,Spring Mvc,Spring Restcontroller,我在一个应用程序中使用@RestControllers,其中所有请求都是POST请求。。。正如我从中了解到的,您不能将单个post参数映射到单个方法参数,而是需要将所有参数包装到一个对象中,然后将此对象用作带有@RequestBody注释的方法参数 @RequestMapping(value="/requestotp",method = RequestMethod.POST) public String requestOTP( @RequestParam(value="idNumber
@RestController
s,其中所有请求都是POST
请求。。。正如我从中了解到的,您不能将单个post参数映射到单个方法参数,而是需要将所有参数包装到一个对象中,然后将此对象用作带有@RequestBody
注释的方法参数
@RequestMapping(value="/requestotp",method = RequestMethod.POST)
public String requestOTP( @RequestParam(value="idNumber") String idNumber , @RequestParam(value="applicationId") String applicationId) {
return customerService.requestOTP(idNumber, applicationId);
无法处理正文{“idNumber”:“345”,“applicationId”:“64536”}的POST
请求
我的问题是,我有很多的
POST
请求,每个请求只有一个或两个参数,创建所有这些对象只是为了接收内部的请求将是一件乏味的事情。。。那么,还有其他类似于处理get请求参数(URL参数)的方法吗?我已经更改了您的代码,请检查
DTO类
public class DTO1 {
private String idNumber;
private String applicationId;
public String getIdNumber() {
return idNumber;
}
public void setIdNumber(String idNumber) {
this.idNumber = idNumber;
}
public String getApplicationId() {
return applicationId;
}
public void setApplicationId(String applicationId) {
this.applicationId = applicationId;
}
}
静止控制器法
@RequestMapping(value="/requestotp",method = RequestMethod.POST)
public String requestOTP( @RequestBody DTO1 dto){
System.out.println(dto.getApplicationId()+" (------) "+dto.getIdNumber());
return "";
}
请求类型——应用程序/json
{“idNumber”:“345”,“applicationId”:“64536”}
或
是的,有两种方法- 首先,您所要做的就是用url附加这些参数,而无需在正文中给出它们。 url类似于-baseurl+/requestotp?idNumber=123&applicationId=123
@RequestMapping(value="/requestotp",method = RequestMethod.POST)
public String requestOTP( @RequestParam(value="idNumber") String idNumber , @RequestParam(value="applicationId") String applicationId) {
return customerService.requestOTP(idNumber, applicationId);
第二,您可以按如下方式使用map
@RequestMapping(value="/requestotp",method = RequestMethod.POST)
public String requestOTP( @RequestBody Map<String,Object> body) {
return customerService.requestOTP(body.get("idNumber").toString(), body.get("applicationId").toString());
@RequestMapping(value=“/requestotp”,method=RequestMethod.POST)
公共字符串requestOTP(@RequestBody-Map-body){
返回customerService.requestOTP(body.get(“idNumber”).toString(),body.get(“applicationId”).toString());
您的请求正文是json格式,您正在接收表单类型的数据。请将请求正文更改为表单类型而不是json类型。我正在从高级rest客户端尝试,我看不到表单类型…您的意思是多部分/表单数据
?不,您应该以表单数据的格式发送数据。您可以在单个类中绑定所有参数,并且接受它为@RequestBody,并使用getter setter根据您的需求获取参数。实际上,我的问题正是如何避免这种方法:)……因为我需要为每个请求创建一个DTO是的,因为您必须将您的请求类型更改为表单数据。您可以在此处发布答案,以便每个请求都能获得e利益我也受到前端(角度)的限制发送JSON请检查我的更新答案,我认为第二个ANS将解决您的问题。:)很好…但是如果我想对参数@Valid
…进行自动验证,该怎么办?我想我将无法使用Map,对吗?是的,如果您将使用Map,那么您必须手动进行验证。如果我的参数之一是字符串数组,该怎么办ppose我可以对管道进行分隔,然后在后端进行拆分,但是否有本机解决方案?
@RequestMapping(value="/requestotp",method = RequestMethod.POST)
public String requestOTP( @RequestBody Map<String,Object> body) {
return customerService.requestOTP(body.get("idNumber").toString(), body.get("applicationId").toString());