org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing

这篇具有很好参考价值的文章主要介绍了org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

报错信息

org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public cn.hp.util.Result cn.hp.controller.ValidateCode.sendSMS(java.lang.String)

控制台输出的是缺少必须的请求正文
发现从前端 走的请求 携带一个参数 到后端没有接收到

前端代码

  axios.post("http://127.0.0.1:8080/validateCode/send4Order/" + telephone).then(res => {}

后端代码

//发送验证码
@RestController
@RequestMapping("/validateCode")
public class ValidateCode {

    @PostMapping(value = "/send4Order/{telephone}")
 public Result sendSMS(@RequestBody String telephone) {

 }

原因:
报错时:后台代码使用@RequestBody 注解报错i 前端发送请求,没有进这个controller
把@RequestBody 换成 @PathVariable 就好了

@RestController
@RequestMapping("/validateCode")
public class ValidateCode {

    @PostMapping(value = "/send4Order/{telephone}")
     public Result sendSMS(@PathVariable String telephone) {
     
     }

@RequestBody

注解@RequestBody接收的参数是来自requestBody中,即请求体。

主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)

最常用的请求体传参是POST请求,所以使用@RequestBody接收数据时,一般都用POST方式进行提交。

请求方式

  • GET请求中,因为没有HttpEntity,所以@RequestBody并不适用。
  • POST请求中,通过HttpEntity传递的参数,必须要在请求头中声明数据的类型Content-Type,

@RequestParam

注解@RequestParam接收的参数是来自requestHeader中,即请求头。

接收的参数是来自HTTP请求体或请求url的QueryString中。

RequestParam可以接受简单类型的属性,也可以接受对象类型。

实质是将Request.getParameter() 中的Key-Value参数Map利用Spring的转化机制ConversionService配置,转化成参数接收对象或字段。

如果在请求中指定contentType: 'application/json;charset=UTF-8’时会出现400错误文章来源地址https://www.toymoban.com/news/detail-436784.html

@RequestParam有三个配置参数:

  • required 表示是否必须,默认为 true,必须。
  • defaultValue 可设置请求参数的默认值。
  • value 为接收url的参数名(相当于key值)。

区别

  • 在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用
  • @RequestBody最多只能有一个,而@RequestParam()可以有多个。

总结

  • 在GET请求中,不能使用@RequestBody。
  • 在POST请求,可以使用@RequestBody和@RequestParam,但是如果使用@RequestBody,对于参数转化的配置必须统一。
  • 如果使用@RequestParam来接受参数,可以在接受参数的model中设置@DateFormat指定所需要接受时间参数的格式。
  • 在使用@RequestParam,不能指定contentType: ‘application/json; charset=UTF-8’
  • 另外,使用@RequestBody接受的参数是不会被Servlet转化统一放在request对象的Param参数集中,@RequestParam是可以的。
  • 一般情况下,推荐使用@RequestParam注解来接受Http请求参数。

POST请求时

  • @RequestBody --> JSON字符串部分
  • @RequestParam --> 请求参数部分
  • application/json格局图
  • form-data、x-www-form-urlencoded格局图

到了这里,关于org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for

    SpringBoot请求接口报错 这是我的实体类 可以发现我的实体类并没有set和get方法才会报错 HttpMediaTypeNotAcceptableException 修改之后添加set和get方法,可以成功运行

    2024年02月12日
    浏览(60)
  • 解决报错:@org.springframework.beans.factory.annotation.Autowired(required=true)

    先把问题贴出来: @org.springframework.beans.factory.annotation.Autowired(required=true) 报这个错是因为: @Autowired(required=true):当使用@Autowired注解的时候,其实默认就是@Autowired(required=true),表示注入的时候,该bean必须存在,否则就会注入失败。 ** ** 解决办法:

    2024年02月13日
    浏览(40)
  • 【错误】A component required a bean of type ‘org.springframework.security.config.annotation.ObjectPostPr

    Description: A component required a bean of type \\\'org.springframework.security.config.annotation.ObjectPostProcessor\\\' that could not be found. Action: Consider defining a bean of type \\\'org.springframework.security.config.annotation.ObjectPostProcessor\\\' in your configuration.   描述: 组件需要“org.springframework.security.configannotation”类型的

    2024年02月13日
    浏览(28)
  • HttpMessageNotReadableException: Required request body is missing:

    完整错误: Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.Object com.example.sx.study.Econtroller.test1(com.example.sx.study.Entity,org.springframework.validation.BindingResult)] 解决办法: 在@RequestBody后加上( required = false ) 虽然通过此方法的确返回了

    2023年04月08日
    浏览(34)
  • 已解决org.springframework.beans.factory.UnsatisfiedDependencyException org.springframework.beans.factor

    已解决org.springframework.beans.factory.UnsatisfiedDependencyException org.springframework.beans.factory.异常的正确解决方法,亲测有效!!! org.springframework.beans.factory.UnsatisfiedDependencyException org.springframework.beans.factor 对于 org.springframework.beans.factory.UnsatisfiedDependencyException 异常,通常是由于依赖注

    2024年02月05日
    浏览(39)
  • org.springframework.context.ApplicationContextException: Failed to start bean ‘org.springframework.a

    使用RabbitMq测试交换机、队列时候报的错 一开始我以为是队列的监听器配置信息和监听器代码写错了,反复查看并没有错误。 然后继续往下看报错,有这俩报错,瞬间顿悟。 ACCESS_REFUSED 这么大个字,看了好几遍居然没想到这方面的错误,大概是配置密码的时候copy错了 hotel

    2024年02月15日
    浏览(44)
  • 报错:org.springframework.jdbc.BadSqlGrammarException:

    //报错 2024-02-24 19:44:10.814 ERROR 6184 --- [nio-9090-exec-5] c.e.exception.GlobalExceptionHandler     : 异常信息: org.springframework.jdbc.BadSqlGrammarException:  根据异常信息,这是一个Spring框架抛出的`BadSqlGrammarException`异常,通常这个异常表示SQL语法错误。 要解决这个问题,你需要做以下几步:

    2024年04月10日
    浏览(37)
  • org.springframework.data.mongodb.UncategorizedMongoDbException

    org.springframework.data.mongodb.UncategorizedMongoDbException: Runner error: Overflow sort stage buffered data usage of 33700095 bytes exceeds internal limit of 33554432 bytes; nested exception is com.mongodb.MongoException: Runner error: Overflow sort stage buffered data usage of 33700095 bytes exceeds internal limit of 33554432 bytes 这个异常通常会在

    2024年02月12日
    浏览(29)
  • org.springframework.util.StopWatch使用说明

    StopWatch是一个简单实用的秒表工具类,可以用来评估代码块的执行时间和性能。 以下是StopWatch的主要用法: 创建StopWatch对象 启动秒表计时 执行需要计时的代码块 停止秒表计时 获取执行时间 间隔计时 可以通过多次调用start()和stop()进行间隔计时。 例如: 然后可以通过getTaskTim

    2024年02月03日
    浏览(28)
  • 关于org.springframework.beans.factory.NoSuchBeanDefinitionException

    这个报错可能是因为: 1. spring的xml配置文件Bean中的id和getBean的id不一致 spring的配置文件中: 而程序中 applicationContext.getBean(“studenta”, Student.class)中的是studenta而spring配置文件的id是student,不一致。 2. 是否是忘记加注解了 @Resource或@Autowired都可以(@Resource是jdk自带的) 3.如果

    2024年02月13日
    浏览(60)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包