@RequestBody不能映射到對象的解決
@RequestBody不能映射到對象
在使用@RequestBody 映射對象時(shí)總是獲取不到j(luò)son穿過來的值
@RequestMapping(value = "/json") public @ResponseBody Items json(@RequestBody Items items) { System.out.println(items); return items; }
public class Items { private Integer id; private String name; private Float price; private String pic; private Date createtime; private String detail; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public Float getPrice() { return price; } public void setPrice(Float price) { this.price = price; } public String getPic() { return pic; } public void setPic(String pic) { this.pic = pic == null ? null : pic.trim(); } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail == null ? null : detail.trim(); } @Override public String toString() { return "Items [id=" + id + ", name=" + name + ", price=" + price + ", pic=" + pic + ", createtime=" + createtime + ", detail=" + detail + "]"; } }
解決方法
在springmvc.xml配置文件加入fastjson庫,代碼如下
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> </bean> </mvc:message-converters> </mvc:annotation-driven>
然后問題就解決了
@RequestBody使用方法(將數(shù)據(jù)映射到j(luò)ava對象上)
將請求的json數(shù)據(jù)映射到@RequestBody 聲明的對象上
1.請求方式如下
將id,name,age 的值映射到對象上
2.對象定義如下
屬性名稱要和json中的名稱對應(yīng)上
@Getter @Setter @ToString public class UserEntity { private Long id; private String name; private int age; }
3.可以看到,json數(shù)據(jù)映射到UserEntity里
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java中進(jìn)制的轉(zhuǎn)換,Byte與16進(jìn)制的轉(zhuǎn)換方法
下面小編就為大家?guī)硪黄猨ava中進(jìn)制的轉(zhuǎn)換,Byte與16進(jìn)制的轉(zhuǎn)換方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11深入理解Java并發(fā)編程之LinkedBlockingQueue隊(duì)列
本文主要介紹了Java并發(fā)編程之LinkedBlockingQueue隊(duì)列,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04IDEA調(diào)試技巧條件斷點(diǎn)實(shí)現(xiàn)步驟詳解
這篇文章主要介紹了IDEA調(diào)試技巧條件斷點(diǎn)實(shí)現(xiàn)步驟詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09SpringBoot?ScheduledTaskRegistrar解決動態(tài)定時(shí)任務(wù)思路詳解
本文將從問題出發(fā),詳細(xì)介紹ScheduledTaskRegistrar類是如何解決動態(tài)調(diào)整定時(shí)任務(wù)的思路,并給出關(guān)鍵的代碼示例,幫助大家快速地上手學(xué)習(xí)2023-02-02jasypt對配置文件的數(shù)據(jù)加密與解密方式
這篇文章主要介紹了jasypt對配置文件的數(shù)據(jù)加密與解密方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Java中easypoi的使用之導(dǎo)入校驗(yàn)
因工作需要,使用easypoi導(dǎo)入表格,并進(jìn)行校驗(yàn),將表格中有問題的地方,給出提示信息,以表格形式返回,下面這篇文章主要給大家介紹了關(guān)于Java中easypoi的使用之導(dǎo)入校驗(yàn)的相關(guān)資料,需要的朋友可以參考下2023-03-03