Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Is @RestController considered a stereotype annotation?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

Everything is pretty much in the title.
2 books are telling me it is and it is not. So I wanted to have your opinion on that.
 
Saloon Keeper
Posts: 12402
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you quote from the books why @RestController wouldn't be a stereotype?

If they say it's because it's not in the org.springframework.stereotype package, that's a really silly reason.

Spring doesn't explicitly define what a stereotype is, but I'd call any annotation a Spring stereotype if that annotation is itself annotated with a stereotype annotation.

@Controller, @Repository and @Service are annotated with @Component, so they are stereotypes. @RestController is annotated with @Controller, so it's a stereotype.
 
Robin Louis
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm studying for the Spring professionnal certification and this is one of the question they provide in their study guide :
"Is @Controller a stereotype? Is @RestController a stereotype?"

And those are the answers I found in 2 books that were recommanded to me to study this certification :

1."You might think at this point, since @RestController is a specialization of @Controller, this makes it a stereotype annotation, right?
Wrong. First, there are five stereotype annotations in Spring: @Component, @Repository, @Service, @Indexed and @Controller and they are grouped under the org.sprigframework.stereotype package.
All of them, except @Indexed are meta-annotated with a single Spring annotation, which means that the meta-annotated annotation can be treated the same as the annotation it is annotated with.
They are part of the stereotype package and have a fundamental role in declaring beans.
@Indexed is a special case because it was added in Spring 5 to indicate that the annotated element represents a stereotype for the index.
This is useful when classpath scanning is replaced with reading a metadata file generated at compile time for identifying bean types.
Anyway, @RestController is different because it is a composed annotation that is meta-annotated with @Controller and @ResponseBody.
Because of that @RestController cannot be treated the same way as @Controller and thus it cannot be a stereotype annotation if it cannot be treated the same as one."
Pivotal Certified Professional Core Spring 5 Developer Exam A Study Guide Using Spring Framework 5, written by Iuliana Cosmina and published by Apress.

2. "Is @Controller a stereotype? Is @RestController a stereotype?
According to the first reference of this section, the @Controller is a stereotype annotation.
In addition, it is located in the org.springframework.stereotype package.
The @RestController annotation is a stereotype annotation, given that its declaration is annotated with the @Controller annotation."
Core Spring 5 Certification in Detail, written by Ivan Krizsan

And since it is a question I might get on my exam, i'm quite puzzled by the different answers I got.
 
Stephan van Hulst
Saloon Keeper
Posts: 12402
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pivotal Certified Professional Core Spring 5 Developer Exam A Study Guide Using Spring Framework wrote:Anyway, @RestController is different because it is a composed annotation that is meta-annotated with @Controller and @ResponseBody.
Because of that @RestController cannot be treated the same way as @Controller and thus it cannot be a stereotype annotation if it cannot be treated the same as one.


I really don't see from what premises the author draws their conclusion.

Nowhere does it say that an annotation is not a stereotype if it can't be treated the same way as @Controller specifically. If that were true, then @Repository and @Service wouldn't by stereotypes either. The author also uses circular reasoning: @RestController is not a stereotype because it can't be treated as a stereotype. By that logic, if I treat it as a stereotype then it's a stereotype.

Spring uses the word "stereotype" very loosely. Basically they say that annotations that indicate that a certain class fulfills a certain role, and cause that class to be discovered by Spring's component scanning, can be treated as stereotypes. @RestController falls squarely within that line of thinking. It clearly indicates that a class fulfills the role of a REST controller, and because it's meta-annotated with @Controller, it causes the class to be picked up by Spring's component scanning.

Personally I would consider any annotation that is (indirectly) meta-annotated with @Component to be a stereotype.
 
Robin Louis
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was really confused by that reasonning aswell. Glad I am not the only one.
Thank you so much for your answer.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will add another quote from Iuliana Cosmina's book:

"In Spring 4.0, the @RestController annotation was introduced. This annotation is basically a combination of @Controller and @ResponseBody, and when used on a class, it assumes that all values returned by the methods in this controller should be bound to the web response body.
The @RestController annotation is a specialized version of the @Controller that does more than just mark a component as being used for REST requests; thus it is not considered a stereotype annotation, and was declared in a different package: org.springframework.web.bind.annotation."

My understanding is that @RestController is not considered a stereotype annotation because:

1. All the other stereotype annotations (@Controller, @Repository and @Service) are from org.springframework.stereotype package, but @RestController is part of org.springframework.web.bind.annotation
2. All the other stereotype annotations (@Controller, @Repository and @Service) are used to mark classes (from which a bean will be created) according to a specific purpose. @RestController not only marks classes according to a specific purpose, but also assumes that all values returned by methods in a controller should be bound to the web response body. (The DispatcherServlet is notified that the result of the execution of a controller does not have to be mapped to a view by annotating the method with @ResponseBody)

This is my current understanding. If you have found other relevant informations (maybe in Spring documentation), please post it.
 
Stephan van Hulst
Saloon Keeper
Posts: 12402
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Spring 5 documentation (emphasis mine):

You can register aspect classes as regular beans in your Spring XML configuration or autodetect them through classpath scanning — the same as any other Spring-managed bean. However, note that the @Aspect annotation is not sufficient for autodetection in the classpath. For that purpose, you need to add a separate @Component annotation (or, alternatively, a custom stereotype annotation that qualifies, as per the rules of Spring’s component scanner).


The fact that they refer to custom stereotype annotations implies that not just the annotations in the org.springframework.stereotype package are stereotypes, but really any annotation that is (indirectly) meta-annotated with @Component.

More importantly: Who the heck cares? Even IF for some unfathomable reason custom annotations that specialize @Component are not themselves called stereotypes, what use would the word "stereotype" be? When would you ever want to refer to just the annotations in the org.springframework.stereotype package, and not also to other custom annotations that serve the same purpose?
 
Stephan van Hulst
Saloon Keeper
Posts: 12402
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch, Adrian!
 
Adrian Ciobanu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also don't see why this naming matter would be so important. Pivotal makes it important by adding this question to their "Spring Professional Certification Study Guide" from May 2019.

I just saw that a guy (Dominik Cebula) in it's Udemy course says that @RestController is considered a stereotype annotation. He lists @Component, @Controller, @Repository, @Service and @RestController as stereotype but does not mention if he consideres this to be the complete list or not.

A small problem is that if I will get this question on the exam, I will not now what Pivotal's expected answer is.

(Thanks for the welcome, Stephan)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic