【问题标题】:SpringBoot Method has too many Body parametersSpringBoot 方法的 Body 参数过多
【发布时间】:2020-08-10 09:56:57
【问题描述】:

我为调用我的 RestApi 创建了 feign 客户端。当我尝试运行我的服务时,我从这个 requestMethod 收到错误 Method has too many Body parameters 对于 @RequestBody 我只使用了 Object 类型,因为每次我都可以发送另一个正文请求。

@RequestMapping(path = "/v1/products/{product}/companies/{companyId}", method = RequestMethod.POST,
            consumes = "application/json", produces = "application/json")
    ResponseEntity<Object> createProduct(URI baseUri,
                                         @HeaderParam("tenant-id") String tenantId,
                                         @PathVariable("product") String product,
                                         @PathVariable("companyId") String companyId,
                                         @RequestBody Object reqBody);

【问题讨论】:

  • 您不能使用Object 作为类型,它需要是特定类型,否则序列化框架无法知道要使用哪个对象。
  • 你可以将requestbody作为字符串,然后根据需要进行解析
  • 您也可以使用 ObjectMapper 将字符串映射到对象,具体取决于条件

标签: java spring-boot feign


【解决方案1】:

你不能使用Object作为类型,请尝试用我们的Class指定。框架不知道使用哪个实体作为响应。

@RequestMapping(path = "/v1/products/{product}/companies/{companyId}", method = RequestMethod.POST,
            consumes = "application/json", produces = "application/json")
    ResponseEntity<MyEntity> createProduct(URI baseUri,
                                         @HeaderParam("tenant-id") String tenantId,
                                         @PathVariable("product") String product,
                                         @PathVariable("companyId") String companyId,
                                         @RequestBody Object reqBody);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 2020-03-30
    • 2017-07-15
    • 2019-07-04
    • 1970-01-01
    • 2016-11-13
    • 2017-03-31
    相关资源
    最近更新 更多