简体   繁体   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. 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.

相关问题 如何传递数据到外键,同时传递数据,我使用邮递员获取空值? - How to pass data into a foreign key , while passing data i am getting null using postman? 在 Postman 的请求正文中传递多个 JSON 数据并使用 Jersy(JXRS) 进入 Java Rest API - Pass multiple JSON data in Request Body of Postman and Get into Java Rest API using Jersy(JXRS) 为什么在控制器中从邮递员请求中收到空值? - Why am I receiving null from postman request in my controller? 如何使用 REST assured 将 API 响应中的值存储为全局变量并将其作为 Cucumber 功能文件中的参数传递给另一个 API - How can i store a value as global variable from an API response and pass it to another API as parameter in Cucumber feature file using REST assured 如何将时间和日期值从邮递员发送到我的 REST API - How to send Time and Date value from Postman to my REST API 我正在尝试将 firebase 实时数据库中的数据检索到我的 recyclerview 中,但数据显示 null - I am trying to retrieve data from firebase realtime database into my recyclerview but data is showing null 当我在MongoDB上使用rest api时,为什么我没有得到确切的数据? - Why I am not getting exact data when I am using rest api with MongoDB? 如何使用邮递员休息客户端将自定义dto作为休息api输入传递 - how to pass custom dto as rest api input using postman rest client AdapterSecurityContext 的客户端注册数据为 null,同时使用 POSTMAN 测试 API - Client registration data of AdapterSecurityContext is null while testing the API using POSTMAN 当我从html表单传递值时,为什么在我的post api期间出现&#39;Column not not null&#39;的错误 - Why am I getting an error for 'Column cannot be null' during my post api when I am passing a value from a html form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM