英文:
'IncompatibleClassChangeError' while returning SseEmitter as response in Java Spring
问题
我正在尝试在Java Spring MVC中使用基于XML配置实现Server Sent Event(SSE)。但是在访问以下URL时,我遇到了以下错误消息:/subscribe-sse
Java版本:
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
Servlet API版本: 4.0.1
spring-webmvc版本: 5.2.0
spring-web版本: 5.3.8
控制器代码:
@GetMapping(path = "/subscribe-sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public SseEmitter subscribe() {
SseEmitter emitter = new SseEmitter(Long.MAX_VALUE);
return emitter;
}
我遇到的错误:
java.lang.IncompatibleClassChangeError: Expected non-static field org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.logger
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:128)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
javax.servlet.http.HttpServlet.service(HttpServlet.java:502)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
javax.servlet.http.HttpServlet.service(HttpServlet.java:596)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
我尝试在Spring Boot中实现Server Sent Event,但遇到了IncompatibleClassChangeError错误。
英文:
I am trying to implement Server Sent Event in java spring MVC with xml based configuration.But I am getting the following error message while hitting the following URL: /subscribe-sse
Java Version:
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
Servelet API: 4.0.1
spring-webmvc:5.2.0
spring-web:5.3.8
Controller:
@GetMapping(path = "/subscribe-sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public SseEmitter subscribe() {
SseEmitter emitter = new SseEmitter(Long.MAX_VALUE);
return emitter;
}
Error I am getting:
java.lang.IncompatibleClassChangeError: Expected non-static field org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.logger
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:128)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
javax.servlet.http.HttpServlet.service(HttpServlet.java:502)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
javax.servlet.http.HttpServlet.service(HttpServlet.java:596)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
I was trying to implement Server Sent Event with spring boot but getting IncompatibleClassChangeError
答案1
得分: 0
我已经按照@Mark Rotteveel的评论解决了这个问题。
问题出在不兼容的Spring版本上。在Spring Web和Web-MVC都使用了5.3.29版本后,问题已经解决。我还额外在web.xml中添加了<async-supported>true</async-supported>
以启用异步请求。
英文:
I have resolved this problem by following @Mark Rotteveel's comment.
The problem was with incompatible spring version. After using 5.3.29 for both spring web and web-mvc the problem has resolved. I additionally added <async-supported>true</async-supported>
on web.xml for async to enable async request.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论