[英]I am using Postman, to pass the data to a REST api but my variable is showing null value
My main question is how to pass a (Map, String) to a REST API, I know if I use @RequestBody all the passed contents are stored to map but what can be done to pass map as well as any other parameters REST API. My main question is how to pass a (Map, String) to a REST API, I know if I use @RequestBody all the passed contents are stored to map but what can be done to pass map as well as any other parameters REST API.
@GetMapping(path="/invoices")
public String invoiceReceived( Map<String,Object> invoice,String format) throws MessagingException {
System.out.println(format); // this prints NULL
return "returnValue";
}
So I tried using PathVariable but they throw exception.所以我尝试使用 PathVariable 但它们抛出异常。 What can be done?
可以做什么?
@GetMapping(path="/invoices/{invoiceData}/{format}")
public String invoiceReceived(@PathVariable("invoiceData") Map<String,Object> invoice,
@PathVariable("format") String format) throws MessagingException {
System.out.println(format); // this prints NULL
return "returnValue";
}
What should I do to accept a map and a variable as input?我应该怎么做才能接受 map 和变量作为输入? And what should be the JSON file look like which should be given as input?
JSON 文件应该是什么样的,应该作为输入给出?
{
"invoiceData":[{"invoiceId":"23642",
"clientName":"Client",
"amount":"23742.67",
"email":"client@abc.com"
}],
"format":"html"
}
This question was identified similar to another question, So I am trying to explain how is this different, I know that I can use @RequestBody to get all the variables in the map, but The call will be made with two parameters some of which will be stored in map but one parameter will be used for another variable.这个问题被确定为与另一个问题相似,所以我试图解释这有何不同,我知道我可以使用@RequestBody 来获取 map 中的所有变量,但是将使用两个参数进行调用,其中一些参数将存储在 map 但一个参数将用于另一个变量。 So how can I send a map along with any other variable?
那么如何发送 map 以及任何其他变量?
I think you can use query strings and path variables.我认为您可以使用查询字符串和路径变量。
If you declare a controller's method like:如果您声明一个控制器的方法,例如:
@GetMapping(path="/invoices")
public String invoiceReceived(@RequestBody Map<String,Object> invoice, @RequestParam String format) {
...
}
the url to which the request is send and the JSON request body will be something like below.请求发送到的 url 和 JSON 请求正文将如下所示。
The url: url:
http://localhost:8080/invoices?format=html
The JSON request body: JSON 请求正文:
{
"invoiceId":"23642",
"clientName":"Client",
"amount":"23742.67",
"email":"client@abc.com"
}
Also you can use a path variable like:您也可以使用路径变量,例如:
http://localhost:8080/invoices/html
@GetMapping(path="/invoices/{format}“)
public String invoiceReceived(@RequestBody Map<String,Object> invoice, @PathVariable String format) {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.