【问题标题】:How to send POST requests to a Spring Boot REST API using Postman form-data?如何使用 Postman 表单数据将 POST 请求发送到 Spring Boot REST API?
【发布时间】:2017-03-29 10:50:34
【问题描述】:

我正在使用 STS 尝试一个非常简单的 Spring Boot + MySql 应用程序:https://github.com/acenelio/person01

使用 raw body 发布一个新的 Person 效果很好。但是,当尝试使用表单数据发布时,我会收到 400 HttpMessageNotReadableException 或 415 HttpMediaTypeNotSupportedException 错误,具体取决于我在 Headers 上设置的 Content-type。

我在这里提供了一些 Postman 打印屏幕:https://sites.google.com/site/stackoverflownelioalves/arqs

我错过了什么?我对 Postman 和 Spring Boot 有点陌生。谢谢。

【问题讨论】:

标签: spring rest spring-boot postman


【解决方案1】:

你可以使用@RequestBody注解:

@RequestMapping("/addInvestorProfile")
    public @ResponseBody Boolean addInvestorProfile(@RequestBody() InvestorProfile profile
, @RequestParam(value = "sessionId") String sessionId) 
  {
            .... your content
            return true;
  }

【讨论】:

    【解决方案2】:

    我不确定您的弹簧靴是哪个版本的。首先,当您没有将consumer 参数添加到@RequestMapping 注释时,几乎所有spring mvcspring boot 控制器方法都将接受所有请求内容类型。所以,也许邮递员有一些错误的用法。我很好地使用弹簧靴和邮递员。

    尝试通过复制请求来比较邮递员的行为curl

    通常发布请求使用curl 看起来像: curl -XPOST UrlOfYourSpringBootEndPoint -d'body of your post' curl here的更多细节。

    【讨论】:

      猜你喜欢
      • 2018-10-29
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-09
      • 1970-01-01
      • 2020-05-23
      相关资源
      最近更新 更多