When to use #ServerEndpoint vs #Controller in Java spring boot framework? - java

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

When a class annotated by #ServerEndpoint is registered to a server-side WebSocket endpoint and every time the corresponding endpoint's WebSocket is connected to the server, its instance is created and managed by JWA implementation.
Classic controllers can be annotated with the #Controller annotation. This is simply a specialization of the #Component class and allows implementation classes to be autodetected through the classpath scanning.
Hope this helps

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

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.

Related

Is it possible use #Service/#Component instead of #Controller annotation SpringMVС?

Is it possible use #Service/#Component instead of #Controller annotation in Spring MVC
( I checked this out and it doesn't work) but why?
The #Controller annotation is a specific type that, like all spring annotations, is derived from the #Component annotation. Note that, depending on use case, it may work, and it may not. Controller classes are often used for mapping server requests to responses.
You can view this Geeks for Geeks article for more info on when to use the #Controller annotation. If you have #RequestMapping annotations in your class, you will not get the same behavior with a Component as you will with Controller.

What will happen if we interchange #service and #repository annotation in the spring MVC

Why we needs to use #service inside the service Implementation and #repository in the DAO Implementation. There are no problem occur when I interchange the #service and #repository annotation in the spring MVC.
According to documentaion #Repository,#Service,#Controller are all synonyms. They all are just specializations of #Component annotation. So, generally, they can be used one instead of other. But ... you should not do this.
First reason: any of these annotations make clear the role of your component in the application. Shows - is this component belongs to the controller, service, or data layer.
Second reason: some of these annotations processed differently by different Spring modules. For example, Spring Data JPA will process #Repository and will try to replace with implementation any interface marked by this annotation. Spring also will apply automatic exception translation to such classes. Another example: Spring Web MVC processes #Controller, and uses classes marked with it in URL mappings.
Actually, in future versions, some modules of Spring could process #Service in a particular way. Not as simple #Component. That's why documentation advises:
It is also possible that #Repository, #Service, and #Controller may
carry additional semantics in future releases of the Spring Framework.
Thus, if you are choosing between using #Component or #Service for
your service layer, #Service is clearly the better choice.
It depends on what you use for the remainder of the framework. In theory nothing changes as the #Service and #Repository annotations are basically #Component annotations. The same could be said for #Controller or #Endpoint (for Spring Ws and there are more).
However they express an intent of what a class is (a service, a repository) and makes it clear to the user to what layer that class belongs.
However if you also use Spring for transaction managemnt then #Repository is also a trigger for adding exception translation to that class (also see the reference guide).
Although nothing has to break it probably will at some point.

#Controller annotation and Controller Classes in Spring MVC

When I am using Spring 3.x
While using annotations its difficult for me to know Which type of Controller class we are going to fetch using this #Controller
With reference to
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/Controller.html
These are implementing Controller class
AbstractController
AbstractUrlViewController
MultiActionController
ParameterizableViewController
ServletForwardingController
ServletWrappingController
UrlFilenameViewController
AbstractWizardFormController
SimpleFormController
However when we are using #Controller annotation in our Spring MVC
program how to know that Our #Controller annotation is implementing
any of these controllers, Kindly anyone explain me
I think you are missing the point here. In the old times to register a controller, your class must have implemented controller interface and choose request mapping type. Sometimes you had to implement lots of code to achieve single request mapping.
Nowadays, when we have annotations, the model has changed. We can handle multiple request types per controller class. Because in single #Controller annotated class we can handle many request mappings.
Controller annotation is a specialized #Component, that tells Spring that inside it will find #RequestMapping handlers. Those handlers can be used either for returning Json, HTML or for uploading files.
Now logic connected with same module can be placed under single controller class, you are more flexible in what you want to achieve. Secondly #Controller enables us to reduce code anount significantly.
You aren't using any of those classes. You confuse the Controller interface with the annotation.
The annotation is just a marker that states that your bean is a Spring Controller and can send an receive HTTP requests.
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/stereotype/Controller.html
The Controller interface was introduced when there were no annotations in the Java language.
Which type of Controller class we are going to fetch using this
#Controller ?
The #Controller annotation is to tell the web container that this is the controller class for requests with urls specified with #RequestMapping(URI). you are going to fetch no class if you are annotating your class with #Controller , you just need to provide a request handler method within the class and annotate it with #RequestMapping , though the controller class can also be annotated with #RequestMapping and in that case , only method inside the class will work as request handler method.
how to know that Our #Controller annotation is implementing any of
these controllers ?
The names of the classes you have mentioned are implementing Controller interface , not that #Controller annotation is implementing anything .Those controller classes are for specific purposes ,as their names suggest .

isHandler Method of RequestMappingHandlerMapping retunrs true for both RequestMapping and Controller annotation

I am looking into Dispatcher Servlet code. Here i found that dispatcher servlet uses HandlerMapping to select the handler for the request. Also, RequestMappingHandlerMapping is used as an implementation for HandlerMapping. Now, isHandlerMethod of RequestMappingHandlerMapping returns true if the bean under consideration has either #Controller or #RequestMapping annotation. If certain bean has only #RequestMapping annotation applied at class level would it still be considered as Handler?.
Any Help would be greatly appreciated.
The #RequestMapping and #Controller annotation have different meanings. The request mapping is used to decide, which class / method is used to handle a request to a speciffic URL. If you look at the source of the #Controller adnotation you will find that it is annotated with #Component itself. This way it can be used to set up the component scan that will register an instance of the class as a bean.
I'm quessing that, since those annotations are usually used together, it's done so that a minute performance gain can be achieved. Also, you could declare your controllers differently, either by java config or xml.
Edit:
I have done a quick prototype with a controller bean declared in java config, without the #Controller annotation. The answer is yes, the method will be used to handle a request, even if the class is not annotated.

Which #controller annotation should I be using with Spring-mvc?

org.springframework.stereotype;
or
org.springframework.web.servlet.mvc;
I had been using the stereotype but just noticed I have another option ?
org.springframework.stereotype.Controller
Is an annotation. It indicates that the annotated class is a controller as well as a candidate for auto-detection (like a #Component). This is the annotation you want to use.
org.springframework.web.servlet.mvc.Controller
Is an interface for implementing controllers. In most simple cases you probably don't want to be using this.

Categories

Resources