springMVC Controller层接收 JSON参数遇到得问题
@RequestBody
使用@RequestBody传参,@RequestBody可以传json
@Autowiredprivate IUserService userService;//在需要使用日志的地方加上这句代码即可private static final Logger logger = LoggerFactory.getLogger(UserController.class);//insertUser方法@PostMapping("/insert")public String insertUser(@RequestBody User user) throws Exception{logger.info("日志 getEmail = {}",user.getMobile());userService.insertUser(user);return "eeee";}
import lombok.Data;@Data
public class User {private int id;private String email;private String mobile;private String username;private String role;
}
在postman里填入得值:
使用此方法遇到的问题:
在spring mvc框架中需要在pom文件中加上下面的内容来引入jackson,否则接受识别不了json。
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.9</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.9.9</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.9.9</version></dependency>
如果使用springboot,它已经自动给你引入看jackson,不需要自己去引入。springMVC就是麻烦!!!!
@RequestParam
@RequestParam用来处理 Content-Type 为 application/x-www-form-urlencoded 编码的内容,Content-Type默认为该属性,也可以接收application/json。
语法:@RequestParam(value=”参数名”,required=”true/false”,defaultValue=””)value:参数名required:是否包含该参数,默认为true,表示该请求路径中必须包含该参数,如果不包含就报错。defaultValue:默认参数值,如果设置了该值,required=true将失效,自动为false,如果没有传该参数,就使用默认值
@PostMapping("/insert")public String insertUser(@RequestParam(value = "id") int id,@RequestParam(value = "email") String email,@RequestParam(value = "username") String username,@RequestParam(value = "role") String role,@RequestParam(value = "mobile") String mobile) throws Exception{User user =new User();user.setId(id);user.setEmail(email);user.setUsername(username);user.setRole(role);user.setMobile(mobile);String getemail = user.getEmail();logger.info("日志 getEmail = {}",user.getMobile());userService.insertUser(user);return "eee";}
在postman里填入得值:
springMVC Controller层接收 JSON参数遇到得问题相关推荐
- @Springboot搭建项目controller层接收json格式的对象失败
今天在使用swagger2测试的时候出错 1.@requestBody注解常用来处理content-type不是默认的application/x-www-form-urlcoded编码的内容,比如说: ...
- java controller json_springMVC的controller层接收前端json数据
1.前端的操作: 对象接收json数据方式:将所需要传的数据转化为json数据,并将这些数据以post方式传到后台的controller层,然后controller层接收json数据,并且是以对象的形 ...
- springmvc 传递和接收数组参数
java url中如何传递数组,springMVC框架controller类如何接收数组参数? 下面介绍一下URL中传递数组参数方法: dd.do?titles[]=col1&titles[] ...
- springboot接收json参数_Springboot + Vue + shiro 实现前后端分离、权限控制
小Hub领读: 嘿嘿,之前我也发了一篇类似的项目,SpringBoot+Vue的项目,还有视频讲解,如果这篇文章看完不懂,不妨去看看我的视频讲解哈,超级详细! 太赞了,SpringBoot+Vue前后 ...
- Java接收json参数
Java接收json参数 import java.util.List; import java.util.Map; import org.springframework.web.bind.annota ...
- Spring Boot笔记-controller接收json参数
以前使用的是这样的方式获取JSON参数的 在controller端参数使用HttpServletRequest HttpServletRequest request 通过他的 request.getI ...
- java 自定义注解 生成json_SpringBoot:自定义注解实现后台接收Json参数
0.需求 在实际的开发过程中,服务间调用一般使用Json传参的模式,SpringBoot项目无法使用@RequestParam接收Json传参 只有@RequestBody支持Json,但是每次为了一 ...
- springboot中service层注入的是实现类,但Controller层接收的是接口
我们习惯这样编写代码: 在service层写接口,然后用实现类去实现接口,并且将实现类注入到容器中 @Service public class AccountServiceImpl implement ...
- SpringMVC 之@RequestBody 接收Json数组对象
1. 摘要 程序流程: 前台使用ajax技术,传递json字符串到后台: 后台使用Spring MVC注解@RequestBody 接受前台传递的json字符串,并返回新的json字符串到前台: 前台 ...
最新文章
- 浅谈Sharepoint权限
- 计算机光盘工作原理,CD-ROM的工作原理
- 朱明亮:参与开源软件让业余时间更有意义
- LogoSharp:Logo语言的C#实现
- uniapp 微信小程序打包 vendor.js过大 导致打包超过2M
- 架构分享--微博架构
- maven配置其他源下载jar包(详细)
- “绿色高效 模块智能”金融业数据中心系列规范发布会将于12月7日召开
- JSON.parse()、eval()、JSON.stringify()、jQuery.parseJSON()的用法
- spss进行灰色关联分析_灰色关联分析模型
- 用于图像识别的编程语言,你知道几个?
- java url 长度限制_GET请求中URL的最大长度限制总结
- 【IMU】BMI160 Driver分析及使用
- JavaScript设计模式——访问者模式
- python摄像头人脸识别代码_Python3利用Dlib19.7实现摄像头人脸识别的方法
- UE4.27 基于composure的虚拟制片
- 做自己的神——极高自由度的功能游戏:我的世界
- linux 下连接 sqlserver
- 怎么关闭excel出现的microsoft office 自定义安装程序提示
- 自用【Python学习记录】——列表、字典