简体   繁体   English

将对象元素强制转换为数组int [] java spring

[英]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 我需要对发送到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]

My source code in Spring boot 我在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: 我的对象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 . 看起来像JSON "idOficinas": [4580,4243]正在转换为Integers List However, you are trying to cast this to String[] int[] . 但是,您试图将其强制转换为String[] int[] These two types are incompatible. 这两种类型不兼容。 Hence you get a ClassCastException . 因此,您将收到ClassCastException

You should be able to resolve your issue by doing something like: 您应该可以通过执行以下操作来解决问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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