‘IncompatibleClassChangeError’ 在 Java Spring 中返回 SseEmitter 作为响应时发生。

huangapple go评论74阅读模式
英文:

'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 &lt;async-supported&gt;true&lt;/async-supported&gt; on web.xml for async to enable async request.

huangapple
  • 本文由 发表于 2023年7月23日 18:24:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定