spring 可以通过@RequestBody 以外的方式spring map POST 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49670209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can spring map POST parameters by a way other than @RequestBody
提问by osama yaccoub
I am using @RestControllers with an application where all requests are POSTrequests ... As I learned from this post, you can't map individual post parameters to individual method arguments, rather you need to wrap all the parameters in an object and then use this object as a method parameter annotated with @RequestBodythus
我在@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);
will not work with a POSTrequest of body {"idNumber":"345","applicationId":"64536"}
不适POST用于 body 请求{"idNumber":"345","applicationId":"64536"}
MY issue is that I have A LOTof POSTrequests , each with only one or two parameters, It will be tedious to create all these objects just to receive the requests inside ... so is there any other way similar to the way where get request parameters (URL parameters) are handled ?
我的问题是,我有很多的POST要求,每个只有一个或两个参数,这将是乏味创建所有这些对象只是为了得到里面的请求......所以有相似的地方获得请求的方式任何其他方式参数(URL 参数)被处理?
回答by Devendra Singh
Yes there are two ways -
是的,有两种方法-
first - the way you are doing just you need to do is append these parameter with url, no need to give them in body. url will be like - baseurl+/requestotp?idNumber=123&applicationId=123
首先 - 您需要做的就是将这些参数附加到 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);
second- you can use map as follows
第二-您可以按如下方式使用地图
@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());
回答by Deedar Ali Brohi
I have change your code please check it
我已经更改了您的代码,请检查
DTO Class
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;
}
}
}
Rest Controller Method
休息控制器方法
@RequestMapping(value="/requestotp",method = RequestMethod.POST)
public String requestOTP( @RequestBody DTO1 dto){
System.out.println(dto.getApplicationId()+" (------) "+dto.getIdNumber());
return "";
}
Request Type -- application/json {"idNumber":"345","applicationId":"64536"}
请求类型——application/json {"idNumber":"345","applicationId":"64536"}
OR
或者
@RequestMapping(value="/requestotp",method = RequestMethod.POST)
public String requestOTP( @RequestBody String dto){
System.out.println(dto);
return "";
}

