简体   繁体   中英

getting java.lang.ClassCastException

I am creating a remote api using spring tool suite api and when running my code I keep getting an error. Been trying to figure out the problem, for hours but can't seem to figure it out.

Error Message:

Caused by: java.lang.ClassCastException: class org.springframework.http.HttpHeaders cannot be cast to class lombok.var (org.springframework.http.HttpHeaders and lombok.var are in unnamed module of loader 'app')
    at com.ncr.aem.controller.ViewIncidentsController.getAllSummary(ViewIncidentsController.java:462)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    ... 40 more

I believe the issue is due to the type casting in the below code snippet:

Code Snippet:

HttpHeaders responseHeaders = new HttpHeaders();
  responseHeaders.set("Baeldung-Example-Header", 
          "Value-ResponseEntityBuilderWithHttpHeaders");
    var headers = (var) new HttpHeaders();
   
    

I am thinking I will need to cast the the HttpHeaders object using the correct data type. I am not too sure what that data type is for the HttpHeaders object. any ideas will be helpful

The error messsage already describes what's wrong:

Caused by: java.lang.ClassCastException: class org.springframework.http.HttpHeaders cannot be cast to class lombok.var (org.springframework.http.HttpHeaders and lombok.var are in unnamed module of loader 'app')

Here, you are trying to cast the HttpHeaders to var type. var is not a type(a class)

  var headers = (var) new HttpHeaders();

I am not too sure what that data type is for the HttpHeaders object. any ideas will be helpful

How about var headers = new HttpHeaders(); . IDK why you needed to cast it in the first place?

Or, HttpHeaders headers = new HttpHeaders();

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