的javascript
$.ajax({
headers : {
'Accept' : 'application/json' ,
'Content-Type' : 'application/json'
},
url : "/realgrid/product/addOd.do",
type : "post",
data : JSON.stringify(JsonOrderDetailValues),
dataType : "json",
success : function(data){
var json = eval(data);
if (json.result == 1) {
alert("insert.");
}else if(json.result == 2){
alert("update.")
}else {
alert("error.");
}
},
error : function(request,status,error){
alert("code:"+request.status+"\n"+"error:"+error);
}
});
的console.log(JsonOrderDetailValues)
[Object, Object, Object, Object]
0: Object
categoryCode: "AC-MV"
categoryName: undefined
costPrice: 168050
date: undefined
modelCode: "YP-GB70"
modelName: undefined
no: 1
number: "201309120932"
total: 88
totalPrice: 14788400
typeCode: "AV"
typeName: undefined
unitPrice: 294500
__proto__: Object
1: Object
2: Object
3: Object
java spring Controller
@RequestMapping (value="/addOd.do",method=RequestMethod.POST)
public ModelAndView addOdMaster(@RequestBody List<OrderDetails> od){
//java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast
System.out.println(od.get(0).getNumber());
Map<String, Integer> result = new HashMap<String,Integer>();
result.put("result", 0);
return new ModelAndView("jsonView",result);
}
VO
public class OrderDetails {
private String number;
private int no;
private String typeCode;
private String categoryCode;
private String modelCode;
private int total;
private int costPrice;
private int unitPrice;
private int totalPrice;
public int getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(int totalPrice) {
this.totalPrice = totalPrice;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getTypeCode() {
return typeCode;
}
public void setTypeCode(String typeCode) {
this.typeCode = typeCode;
}
public String getCategoryCode() {
return categoryCode;
}
public void setCategoryCode(String categoryCode) {
this.categoryCode = categoryCode;
}
public String getModelCode() {
return modelCode;
}
public void setModelCode(String modelCode) {
this.modelCode = modelCode;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getCostPrice() {
return costPrice;
}
public void setCostPrice(int costPrice) {
this.costPrice = costPrice;
}
public int getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(int unitPrice) {
this.unitPrice = unitPrice;
}
}
错误消息
FATAL: Servlet.service() for servlet [dispatcher] in context with path [/realgrid] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.realgrid.productControl.model.OrderDetails] with root cause
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.realgrid.productControl.model.OrderDetails
at com.realgrid.productControl.controller.ProductControlController.addOdMaster(ProductControlController.java:181)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
为什么不呢?
答案 0 :(得分:12)
在旧版本的Spring中,Spring无法(或者更确切地说不会)确定
@RequestBody List<OrderDetails> od
具有通用类型OrderDetails
,因此Jackson(JSON反序列化器)将使用LinkedHashMap
作为默认类型。类型安全性将像您的情况一样中断,因为所有这整个请求处理调用(@RequestMapping
方法及其参数)都是通过反射完成的。
这些版本的解决方案是创建自己的类型
public class OrderDetailsList extends ArrayList<OrderDetails> {}
并将其用作参数
@RequestBody OrderDetailsList od
(或升级到Spring 4。)