【问题标题】:Send string as json with GET request with POSTMAN通过 POSTMAN 的 GET 请求将字符串作为 json 发送
【发布时间】:2020-09-15 18:12:57
【问题描述】:

我有一个 Java Spring API,它需要 2 个参数,一个简单的字符串和一个对象:

@RequestMapping(value = "list", method = RequestMethod.GET)
public ResponseEntity<ListResource> getList(@RequestParam("agentName") String agentName,
                                            @RequestParam("paginationInfo") PaginationInfoList paginationInfo {

       //After http request i expect to have here my java Object PaginationInfoList ready to use

    }

我正在尝试使用 Postman 发送 http GET 请求,但出现此错误,然后我想我没有以正确的方式发送数据对象“paginationInfo”。

"Failed to convert value of type 'java.lang.String' to required type 'com.pippo.prova.test.model.in.PaginationInfoList'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.pippo.prova.test.model.in.PaginationInfoList': no matching editors or conversion strategy found"

由于我无法更改发送方式,事实上必须是 GET 并且我必须使用@ReuqestParam,我不知道如何在 postman 参数中发送 json 数据。我正在尝试这个以及其他选项,但总是出错。

【问题讨论】:

  • 您可以将第二个参数作为字符串并将字符串传递给 requestParam i postman 并在得到它之后将 String 转换为 Object 。
  • 好的,但是通过这种方式,我必须在 Java 中从字符串映射到对象,并且我想要一个 READY java 对象。
  • 你必须直接传递它而不用json字符串。喜欢argentNamepageSize = // pageNumber = // @MatteoBruni
  • Spring 会将其绑定到PaginationInfoList 类。 @MatteoBruni

标签: java json spring-boot api postman


【解决方案1】:

您可以将请求参数绑定到一个对象。在邮递员中,您将有 3 个参数(“agentName”、“pageSize”和“pageNumber”),您的控制器将收到 2 个对象

public ResponseEntity<ListResource> getList(@RequestParam("agentName") String agentName,
                                        @Valid PaginationInfoList paginationInfo)

http://dolszewski.com/spring/how-to-bind-requestparam-to-object/

【讨论】:

    猜你喜欢
    • 2021-02-17
    • 1970-01-01
    • 2021-06-24
    • 2016-03-28
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 2016-02-17
    • 2016-07-03
    相关资源
    最近更新 更多