Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use @ServerEndpoint vs @Controller in Java spring boot framework?

Can someone point out the differences between the 2 and when it is appropriate to use which one?

like image 330
locke14 Avatar asked Sep 16 '25 01:09

locke14


2 Answers

@ServerEndPoint is an annotation for the web socket, and @Controller is an annotation for the web. (Similarly there is @RestController.)

like image 104
WonChul Heo Avatar answered Sep 17 '25 15:09

WonChul Heo


Maybe this article can help:

https://spring.io/blog/2013/05/23/spring-framework-4-0-m1-websocket-support

@ServerEndpoint: If decorated with @ServerEndpoint, the container ensures availability of the class as a WebSocket server listening to a specific URI space

@ServerEndpoint(value="/chat/{username}")
public class ChatEndpoint {
----
}

@Controller: If decorated with @Controller annotation is an annotation used in Spring MVC framework (the component of Spring Framework used to implement Web Application). The @Controller annotation indicates that a particular class serves the role of a controller. The @Controller annotation acts as a stereotype for the annotated class, indicating its role. The dispatcher scans such annotated classes for mapped methods and detects @RequestMapping annotations.

like image 28
Dinesh K Avatar answered Sep 17 '25 16:09

Dinesh K