av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

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

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

問題描述

使用spring mvc時,RequestBody中json轉(zhuǎn)換成對象失敗,因為對象中包含list,如果我去掉list,就能轉(zhuǎn)換成功。

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

public class OrderDishes { // 就餐人數(shù) private int peopleNum; // 單個菜品 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}

錯誤信息:

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)部類實例會默認(rèn)有一個上一級類的實例的引用// 非靜態(tài)內(nèi)部類的實例化是這樣的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)系只是在同一個文件中OrderDishes orderDishes = new OrderDishes(); // 外部類Dishes dish = new Dishes(); // 非靜態(tài)內(nèi)部類,正常方式的實例化

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

求點贊和采納~~

回答2:

字符串轉(zhuǎn)對象時如果對象帶有有參數(shù)構(gòu)造方法,那么必須要增加一個無參數(shù)的構(gòu)造方法,不然通過反射創(chuàng)建對象時會報錯,因為需要參數(shù)

回答3:

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

標(biāo)簽: java
主站蜘蛛池模板: 刘亦菲国产毛片bd | 成人在线观看免费视频 | 精品色 | 婷婷激情综合 | 国产成人免费在线观看 | 亚洲一区二区电影在线观看 | 国产成人精品综合 | 日韩美女在线看免费观看 | 青春草91| 日韩欧美一区二区三区免费观看 | 99爱国产| 婷婷午夜天 | 久久精品亚洲 | 久久高清精品 | 久久久国产一区二区三区 | 亚洲福利在线观看 | 黄色毛片在线观看 | 亚洲成人国产精品 | 久久伊人影院 | 国产一区二区影院 | www.xxxx欧美 | 日韩欧美不卡 | 成人精品国产一区二区4080 | 免费特级黄毛片 | 久久er精品 | 日韩一区二区三区四区五区六区 | 欧美在线视频一区 | 成人精品区 | 亚洲精品电影网在线观看 | 无码日韩精品一区二区免费 | 欧美三级电影在线播放 | 精品在线免费观看视频 | 国产精品不卡 | 99亚洲精品| 精品一区二区三区四区视频 | 国产精品亚洲综合 | 色在线视频网站 | 久久国产精品精品 | 亚洲视频免费在线播放 | 中文字幕亚洲精品 | 久久久精品一区二区三区 |