亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

java - 使用spring mvc時(shí),RequestBody中json轉(zhuǎn)換成對象失敗,對象中包含list

瀏覽:80日期:2023-12-22 15:27:52

問題描述

使用spring mvc時(shí),RequestBody中json轉(zhuǎn)換成對象失敗,因?yàn)閷ο笾邪琹ist,如果我去掉list,就能轉(zhuǎn)換成功。

代碼:java - 使用spring mvc時(shí),RequestBody中json轉(zhuǎn)換成對象失敗,對象中包含listDTO類代碼:

public class OrderDishes { // 就餐人數(shù) private int peopleNum; // 單個(gè)菜品 private List<Dishes> variant; // 套餐 private List<Dishes> product; // 商品總額 private BigDecimal totalAmount; // 折扣金額 private BigDecimal discountAmount; // 備注 private String memo; // 是否推遲下單 private boolean isPostpone; public OrderDishes(int peopleNum, List<Dishes> variant, List<Dishes> product, BigDecimal totalAmount, BigDecimal discountAmount, String memo, boolean isPostpone) {this.peopleNum = peopleNum;this.variant = variant;this.product = product;this.totalAmount = totalAmount;this.discountAmount = discountAmount;this.memo = memo;this.isPostpone = isPostpone; } public OrderDishes() { } public int getPeopleNum() {return peopleNum; } public void setPeopleNum(int peopleNum) {this.peopleNum = peopleNum; } public List<Dishes> getVariant() {return variant; } public void setVariant(List<Dishes> variant) {this.variant = variant; } public List<Dishes> getProduct() {return product; } public void setProduct(List<Dishes> product) {this.product = product; } public BigDecimal getTotalAmount() {return totalAmount; } public void setTotalAmount(BigDecimal totalAmount) {this.totalAmount = totalAmount; } public BigDecimal getDiscountAmount() {return discountAmount; } public void setDiscountAmount(BigDecimal discountAmount) {this.discountAmount = discountAmount; } public String getMemo() {return memo; } public void setMemo(String memo) {this.memo = memo; } public boolean isPostpone() {return isPostpone; } public void setPostpone(boolean postpone) {isPostpone = postpone; } public class Dishes {private Long productID;private int quantity;private String memo;private BigDecimal addedPrice;public BigDecimal getAddedPrice() { return addedPrice;}public void setAddedPrice(BigDecimal addedPrice) { this.addedPrice = addedPrice;}public Long getProductID() { return productID;}public void setProductID(Long productID) { this.productID = productID;}public int getQuantity() { return quantity;}public void setQuantity(int quantity) { this.quantity = quantity;}public String getMemo() { return memo;}public void setMemo(String memo) { this.memo = memo;} }}

傳入json:

{ 'peopleNum': 1, 'variant': [ { 'productID': 269, 'quantity': 1, 'memo': 'string', 'addedPrice': 0 } ], 'product': [ { 'productID':469 , 'quantity': 1, 'memo': 'string', 'addedPrice': 0 } ], 'totalAmount': 1000, 'discountAmount': 0, 'memo': 'string', 'postpone': true}

錯(cuò)誤信息:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: No suitable constructor found for type [simple type, class cc.toprank.byd.pad.dto.OrderDishes$Dishes]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?) at [Source: java.io.PushbackInputStream@5607eee4; line: 5, column: 7] (through reference chain: cc.toprank.byd.pad.dto.OrderDishes['variant']->java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class cc.toprank.byd.pad.dto.OrderDishes$Dishes]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?) at [Source: java.io.PushbackInputStream@5607eee4; line: 5, column: 7] (through reference chain: cc.toprank.byd.pad.dto.OrderDishes['variant']->java.util.ArrayList[0]) at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:229) at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:213) at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:197) at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:147) at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:125) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:99) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)

問題解答

回答1:

將Dishes設(shè)置成靜態(tài)內(nèi)部類如這樣

public static class Dishes{}

內(nèi)部類

// 非靜態(tài)內(nèi)部類實(shí)例會(huì)默認(rèn)有一個(gè)上一級類的實(shí)例的引用// 非靜態(tài)內(nèi)部類的實(shí)例化是這樣的OrderDishes orderDishes = new OrderDishes(); // 外部類Dishes dish = orderDishes.new Dishes(); // 非靜態(tài)內(nèi)部類

靜態(tài)內(nèi)部類

// 如果是靜態(tài)內(nèi)部類則內(nèi)部類和外部類沒有關(guān)聯(lián)關(guān)系// 他們的關(guān)系只是在同一個(gè)文件中OrderDishes orderDishes = new OrderDishes(); // 外部類Dishes dish = new Dishes(); // 非靜態(tài)內(nèi)部類,正常方式的實(shí)例化

JSON反序列化失敗的原因是序列化框架不知道Dishes是內(nèi)部類,所以創(chuàng)建對象失敗了

求點(diǎn)贊和采納~~

回答2:

字符串轉(zhuǎn)對象時(shí)如果對象帶有有參數(shù)構(gòu)造方法,那么必須要增加一個(gè)無參數(shù)的構(gòu)造方法,不然通過反射創(chuàng)建對象時(shí)會(huì)報(bào)錯(cuò),因?yàn)樾枰獏?shù)

回答3:

內(nèi)部類沒有不帶參數(shù)構(gòu)造方法,默認(rèn)生成的構(gòu)造方法帶參數(shù),參數(shù)為外層類對象

標(biāo)簽: java
主站蜘蛛池模板: 91精品影视 | 91短视频版在线观看www免费 | 日韩欧美亚洲国产高清在线 | 欧美成人v视频免费看 | 一级特黄国产高清毛片97看片 | 久久精品国产69国产精品亚洲 | 外国三级毛片 | 日韩中文字幕免费观看 | 综合色吧 | 欧美啪啪网站 | 亚洲第一欧美 | 日韩欧美国产高清在线观看 | 精品久久综合一区二区 | 色屁屁一区二区三区视频国产 | 手机在线看黄色 | 亚洲欧美日韩国产综合 | 香蕉成视频片在线观看 | 91果冻传媒文化有限公司 | 欧美特黄a级高清免费看片 欧美特黄a级猛片a级 | 国产精品一区久久 | 久久精品国产99久久3d动漫 | 亚洲日本欧美产综合在线 | 91网视频在线观看 | 久久精品视频在线观看 | 国产a不卡片精品免费观看 国产a毛片高清视 | 亚洲一二三区久久五月天婷婷 | 你懂的在线免费 | 91精品欧美一区二区三区 | 黄色成年视频 | 亚洲精品三区 | 99久久精品国产一区二区成人 | 亚洲色图18p | 国产成人综合久久亚洲精品 | 久久精品视频久久 | 日韩中文字幕电影在线观看 | 免费的黄色小视频 | 欧洲成人免费高清视频 | 国产精品视频免费观看 | 99久久精品国产麻豆 | 成年美女黄网站色视频大全免费 | 做久爱视频在线观看 |