java - 使用spring mvc時(shí),RequestBody中json轉(zhuǎn)換成對象失敗,對象中包含list
問題描述
使用spring mvc時(shí),RequestBody中json轉(zhuǎn)換成對象失敗,因?yàn)閷ο笾邪琹ist,如果我去掉list,就能轉(zhuǎn)換成功。
代碼:DTO類代碼:
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ù)為外層類對象
