RequestBody

SpringMVC之@RequestBody,@ResponseBody,@RequestParam用法

流过昼夜 提交于 2020-04-13 16:17:35
【今日推荐】:为什么一到面试就懵逼!>>> 1.@RequestBody,@ResponseBody 在使用@RequestBody和@ResponseBody之前需要先配置,一般是JSON数据和实体对象之间的转化,springMVC.xml配置文件需要如下配置: <mvc:default-servlet-handler /> <!-- java对象和json之间的转化--> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> Server端: @RequestMapping(value = "/ajax",method =

Spring MVC RequestBody

流过昼夜 提交于 2019-12-02 02:34:49
上一节小博老师给大家详细讲解了RequestMapping的具体使用方法以及各个参数的含义。今天小博老师继续给大家讲解RequestBody如何使用。 @ RequestBody 用于读取Request请求的body数据,并使用HttpMessageConverter把数据内容解析成目标对象,然后把解析的对象赋值给 controller方法中的对应参数。 适用场景见下表: 在content-type为application/json,application/xml时,只能适用@RequestBody注解进行参数解析。 注意事项: 1. @RequestBody接收的是一个Json对象的字符串,而不是一个Json对象。在进行对象封装后,需要使用JSON.stringify(data)的把对象变成字符串,然后才能调用ajax提交。 2. ajax请求的时候也要指定dataType: "json",contentType:"application/json" 。 实例: 前端代码 $.ajax({ type: “post”, contentType:”application/json”, url: “boweifeng/save”, data: JSON.stringify({name:” 博为峰 ”}), success: function(data){ } }) 后端代码