简体   繁体   中英

Cast object element to array int[] java spring

Good afternoon.

I need to make a cast of the "idOffices" value of the object I send to the web service, but I get the following error

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]

My source code in 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;
}

My object JSON:

   {
    "idCoordinador": 1,
    "idOficinas": [4580,4243],
    "ip": "xx.xx.x.xx",
    "usuario": "IBMUSER"
   }

I do not understand why the error, if I'm sending an array and depending on what I see, it also returns an array.

Thanks

It looks like the JSON "idOficinas": [4580,4243] is being converted to a List of Integers . However, you are trying to cast this to String[] int[] . These two types are incompatible. Hence you get a ClassCastException .

You should be able to resolve your issue by doing something like:

List<Integer> oficinas = (List<Integer>)data.get("idOficinas")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM