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

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

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

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

問(wèn)題描述

使用spring mvc時(shí),RequestBody中json轉(zhuǎn)換成對(duì)象失敗,因?yàn)閷?duì)象中包含list,如果我去掉list,就能轉(zhuǎn)換成功。

代碼:java - 使用spring mvc時(shí),RequestBody中json轉(zhuǎn)換成對(duì)象失敗,對(duì)象中包含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)

問(wèn)題解答

回答1:

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

public static class Dishes{}

內(nèi)部類

// 非靜態(tài)內(nèi)部類實(shí)例會(huì)默認(rèn)有一個(gè)上一級(jí)類的實(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)部類和外部類沒(mé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)建對(duì)象失敗了

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

回答2:

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

回答3:

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

標(biāo)簽: java
主站蜘蛛池模板: 久久视频这里只精品3国产 久久视热这只是精品222 | 欧美成人免费全网站大片 | 日韩黄色一级 | 九九色综合| 伊人久久国产精品 | 国产成人啪精品视频免费网站软件 | 国产精品久久久久久福利69堂 | 人做人爱视频欧美在线观看 | 一级做a爰片性色毛片黄书 一级做a爰片性色毛片新版的 | 乱人伦99久久 | 久久精品国产清自在天天线 | 国产精品高清一区二区不卡 | 香港性特级黄录像片 | 国产欧美成人免费观看视频 | 亚洲精品专区一区二区欧美 | 免费看黄色片 | 国产一二三区在线 | 久久香蕉国产线看观看8青草 | 欧美亚洲免费久久久 | 我要看一级毛片 | 九九精品免费观看在线 | 黄片一级毛片 | 国产精选第一页 | 国产国拍亚洲精品av | 欧美日本亚洲国产一区二区 | 日本aaaa级毛片在线看 | 亚洲制服另类 | 国产精品嫩草影院88v | 日韩精品免费在线观看 | 窝窝午夜精品一区二区 | 欧美黑人一级做a爱性色 | 三级黄在线 | 自拍 欧美 在线 综合 另类 | 国产一级一级 | 国产日韩视频 | 黄色成人毛片 | 免费又黄又硬又大爽日本 | 欧美国产精品日韩在线 | 日韩在线观看视频黄 | 九九免费精品视频 | 亚洲人成网站在线在线 |