下午好。
我需要对发送到Web服务的对象的“ idOffices”值进行强制转换,但是出现以下错误
java.lang.ClassCastException: java.util.ArrayList cannot be cast to [I
at controlador.controlador.arrancarOficinas(controlador.java:70) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_181]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:888) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:664) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
我在Spring Boot中的源代码
@RequestMapping(value = "/arrancarOficinas", method = RequestMethod.PUT)
public Map<Object, Object> arrancarOficinas(@RequestBody Map<String, Object> data) {
HashMap<Object, Object> datares = new HashMap<>();
int[] oficinas = (int[])data.get("idOficinas");
System.out.println(oficinas);
//datares = (HashMap<Object, Object>) servicios.arrancarOficinas(Integer.parseInt(data.get("idCoordinador").toString()), (int[]) data.get("idOficinas"),data.get("usuario").toString(),data.get("ip").toString());
return datares;
}
我的对象JSON:
{
"idCoordinador": 1,
"idOficinas": [4580,4243],
"ip": "xx.xx.x.xx",
"usuario": "IBMUSER"
}
我不明白为什么会出错,如果我正在发送一个数组,并且根据所看到的内容,它还会返回一个数组。
谢谢
答案 0 :(得分:2)
看起来JSON "idOficinas": [4580,4243]
正在转换为List
的{{1}}。但是,您试图将其强制转换为 Integers
String[]
。这两种类型不兼容。因此,您得到一个int[]
。
您应该可以执行以下操作来解决您的问题:
ClassCastException