繁体   English   中英

我正在使用 Postman 将数据传递给 REST api 但我的变量显示 Z37A6259CC0C1DAE0BDZA 值

[英]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.

@GetMapping(path="/invoices")
public String invoiceReceived( Map<String,Object> invoice,String format) throws MessagingException {
        System.out.println(format); // this prints NULL
        return "returnValue";
}

所以我尝试使用 PathVariable 但它们抛出异常。 可以做什么?

@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";
}

我应该怎么做才能接受 map 和变量作为输入? JSON 文件应该是什么样的,应该作为输入给出?

   {
        "invoiceData":[{"invoiceId":"23642",
        "clientName":"Client",
        "amount":"23742.67",
        "email":"client@abc.com"
        }],
        "format":"html"
    }

这个问题被确定为与另一个问题相似,所以我试图解释这有何不同,我知道我可以使用@RequestBody 来获取 map 中的所有变量,但是将使用两个参数进行调用,其中一些参数将存储在 map 但一个参数将用于另一个变量。 那么如何发送 map 以及任何其他变量?

我认为您可以使用查询字符串和路径变量。
如果您声明一个控制器的方法,例如:

@GetMapping(path="/invoices")
public String invoiceReceived(@RequestBody Map<String,Object> invoice, @RequestParam String format)  {
  ...
}

请求发送到的 url 和 JSON 请求正文将如下所示。

url:

http://localhost:8080/invoices?format=html

JSON 请求正文:

{
  "invoiceId":"23642",
  "clientName":"Client",
  "amount":"23742.67",
  "email":"client@abc.com"
}


您也可以使用路径变量,例如:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM