{"id":251,"date":"2023-10-19T16:22:30","date_gmt":"2023-10-19T15:22:30","guid":{"rendered":"https:\/\/onlinecodinghelp.com\/?p=251"},"modified":"2023-10-30T14:10:38","modified_gmt":"2023-10-30T14:10:38","slug":"spring-stereotype","status":"publish","type":"post","link":"https:\/\/onlinecodinghelp.com\/index.php\/2023\/10\/19\/spring-stereotype\/","title":{"rendered":"3. Mastering Spring: Unleash the Power of Stereotype Annotations in Spring Framework"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Stereotype Annotation Hierarchy:<\/h2>\n\n\n\n<p>The stereotype annotations are generally used to create bean(instance) of the Class.  <\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"811\" data-id=\"263\" src=\"https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/10\/Mind-map-1024x811.jpeg\" alt=\"Stereotype Annotation\" class=\"wp-image-263\" srcset=\"https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/10\/Mind-map-1024x811.jpeg 1024w, https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/10\/Mind-map-300x238.jpeg 300w, https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/10\/Mind-map-768x609.jpeg 768w, https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/10\/Mind-map-1536x1217.jpeg 1536w, https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/10\/Mind-map-2048x1623.jpeg 2048w, https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/10\/Mind-map-150x119.jpeg 150w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/figure>\n\n\n\n<p><strong>@<\/strong><b>Compo<\/b><strong>nent:<\/strong> &#8211; It is a class level annotation. It is used to mark a Java class as a bean. A Java class annotated with <strong>@Component<\/strong> is found during the class path. The spring framework pick it up and configure it in the application context as a <strong>Spring Bean<\/strong>. <\/p>\n\n\n\n<p><strong>@<\/strong><b>Configu<\/b><strong>ration: &#8211;<\/strong> It is a class level annotation. The class annoted with <strong>@Configuration<\/strong> used by spring Containers a source of bean definitions.<\/p>\n\n\n\n<p><strong>@<\/strong><b>Repository: &#8211;<\/b>It is a class level annotation. <strong>@Repository<\/strong>&nbsp;annotation is a specialization of&nbsp;<strong>@Component<\/strong>&nbsp;annotation which is used to indicate that the class provides the mechanism for storage, retrieval, update, delete and search operation on objects. The repository is a <strong>DAOs <\/strong>(Data Access Object) that access the database directly. Though it is a specialization of <strong>@Component<\/strong> annotation, so Spring Repository classes are autodetected by spring framework through classpath scanning. <\/p>\n\n\n\n<p><strong>@<\/strong><b>Service:<\/b> &#8211; It is class level annotation. It is also a specialization of&nbsp;<strong>@Component Annotation<\/strong>&nbsp;like the<strong>&nbsp;@Repository Annotation<\/strong>. It tells the spring that class contains the <strong>business logic<\/strong>.<\/p>\n\n\n\n<p><strong>@Controller: &#8211;<\/strong> It is class level annotation. it is also specialization of <strong>@Component<\/strong> <strong>Annotation<\/strong>. It marks a class as a web request handler. It is often used to serve web pages. By default, it returns as String that indicates which route to redirect. It is mostly used with <strong>@RequestMapping<\/strong> annotation.<\/p>\n\n\n\n<p><strong>@<\/strong><b>RestController: &#8211;<\/b> It can be considered as a combination of <strong>@Controller<\/strong> and <strong>@ResponseBody<\/strong> annotations. The <strong>@RestController<\/strong> annotation is itself annoted with <strong>@ResponseBody<\/strong> annotation.                                It eliminates the need for annoting each method with <strong>@<\/strong><b>Respons<\/b><strong>eBody.<\/strong><\/p>\n\n\n\n<p class=\"has-large-font-size\"><strong>Spring Boot Annotations<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-3 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"787\" data-id=\"267\" src=\"https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/10\/Blank-board-1-1024x787.jpeg\" alt=\"\" class=\"wp-image-267\" srcset=\"https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/10\/Blank-board-1-1024x787.jpeg 1024w, https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/10\/Blank-board-1-300x231.jpeg 300w, https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/10\/Blank-board-1-768x590.jpeg 768w, https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/10\/Blank-board-1-1536x1181.jpeg 1536w, https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/10\/Blank-board-1-2048x1574.jpeg 2048w, https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/10\/Blank-board-1-150x115.jpeg 150w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/figure>\n\n\n\n<p><strong>@SpringBootApplication<\/strong> :- <strong>@SpringBootApplication<\/strong> annotation is combination of three annotations          <strong>@EnableAutoConfiguration<\/strong>, <strong>@ComponentScan<\/strong>, and <strong>@SpringBootConfiguration<\/strong>.                                                       <\/p>\n\n\n\n<p>          <\/p>\n\n\n\n<p><strong>@<\/strong><b>ComponentSc<\/b><strong>an: &#8211;<\/strong> The component scan annotation is scan all the classes in package and its sub packages and create beans if the class is annotated with stereotype annotations.                                                                                                             <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-large-font-size\"><strong>Spring MVC And REST Annotations<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>@RequestMapping :-<\/strong> It Is used to map the web requests . It has many optional elements like consumes , header , method , name , params , path , produces , and values . We use it with the class as well as the method.<\/p>\n\n\n\n<p><strong>@GetMapping :-<\/strong> It maps the <strong>HTTP GET<\/strong> requests on the specific handle method . It is used for data <strong>RETRIEVE<\/strong>.<\/p>\n\n\n\n<p><strong>@<\/strong><b>PostMapping: &#8211;<\/b> It maps the <strong>HTTP POST<\/strong> request on the specific handler method. It is used for <strong>ADD<\/strong>.<\/p>\n\n\n\n<p><strong>@PutMapping: &#8211;<\/strong>It maps the <strong>HTTP PUT<\/strong> request on the specific handler method. It is used for <strong>UPDATE<\/strong>.<\/p>\n\n\n\n<p><strong>@<\/strong><b>DeleteMapping: &#8211;<\/b> It maps the <strong>HTTP DELETE<\/strong> requests on the specific handler method. It is used for <b>D<\/b><strong>ELETE.<\/strong><\/p>\n\n\n\n<p><strong>@RequestBody: &#8211;<\/strong> It is used to bind <strong>HTTP<\/strong> request with an object in a method parameter. Internally it uses <strong>HTTP<\/strong> <strong>Message Converter<\/strong> to convert the body of the request. When we annotate a method parameter with <strong>@<\/strong><b>Request<\/b><strong>Body,<\/strong> the spring framework binds the incoming <strong>HTTP<\/strong> request to the body parameters.<\/p>\n\n\n\n<p><strong>@ResponseBody: &#8211;<\/strong>It binds the method return value to the response body. It tells the spring boot framework to serialize a return an object into <strong>JSON<\/strong> and <strong>XML<\/strong> format.<\/p>\n\n\n\n<p><strong>@PathVariable: &#8211;<\/strong> It is used to extract the values from the <strong>URL<\/strong>. It is most suitable for the <strong>REST<\/strong>ful web service, where the <strong>URL<\/strong> contains a path variable. We can define multiple <strong>@PathVariable<\/strong> in a method.<\/p>\n\n\n\n<p><strong>@RequestParam<\/strong>: &#8211; It is used to extract the <strong>Query<\/strong> parameters from the <strong>URL<\/strong>. It is also Known as <strong>Query<\/strong> parameter. It can specify default values if the query parameter is not present in the <strong>URL<\/strong>.<\/p>\n\n\n\n<p><strong>@RestController: &#8211;<\/strong> It can be considered as a combination of <strong>@Controller<\/strong> and <strong>@ResponseBody<\/strong> annotations. The <strong>@RestController<\/strong> annotation is itself annoted with the <strong>@<\/strong><b>ResponseBod<\/b><strong>y <\/strong>annotation. It eliminates the need for annoting each method with <strong>@ResponseBody<\/strong>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-large-font-size\"><strong>@RequestMapping And Its Attributes<\/strong><\/p>\n\n\n\n<p>The <strong>@RequestMapping<\/strong> annotation is a core annotation in Spring Boot that is used to map a specific <strong>HTTP<\/strong> request method to a specific Java method in web application. This means that when a request is sent to the web application, Spring Boot will use the <strong>@RequestMapping<\/strong> annotation to determine which Java method should handle the request.<\/p>\n\n\n\n<p>The <strong>@RequestMapping<\/strong> annotation can be used to specify the request type like <strong>GET, POST, PUT, DELETE <\/strong>this will handle the request. for example, if you want to handle <strong>GET <\/strong>request to the &#8220;<strong>\/contact<\/strong>&#8221; URL path, there is two ways to handle specific request you can use <strong>@RequestMapping (value = &#8220;\/contact&#8221;, method=RequestMethod.<em>GET<\/em>)<\/strong> this will tell Spring Boot that this Java method should handle <strong>GET<\/strong> request to the &#8220;<strong>\/contact<\/strong>&#8221; path. You can also use <strong>@GetMapping (value = &#8220;\/contact&#8221;)<\/strong> both annotation handle <strong>GET<\/strong> request. In <strong>@GetMapping<\/strong> annotation there is <strong>@RequestMapping<\/strong> annotation which is working Internally like this<strong> @RequestMapping (method = RequestMethod.<em>GET<\/em>)<\/strong>.<\/p>\n\n\n\n<p>The <strong>@RequestMapping<\/strong> annotation is commonly used in RESTful web services to map <strong>HTTP<\/strong> request methods to specific Java methods that handle those requests. <strong>For example<\/strong>, a <strong>GET<\/strong> request to the <strong>\/contact<\/strong> URL path can be used to retrieve the list of contact from a database, while <strong>POST<\/strong> request to the same URL path can be used to create new contact in the database. Additionally, Spring Boot provides other related annotations, such as <strong>@GetMapping,<\/strong> <strong>@PostMapping, @PutMapping, @<\/strong><b>DeleteMappi<\/b><strong>ng<\/strong>. This annotation has specific <strong>HTTP<\/strong> methods in <strong>@RequestMapping<\/strong>.<\/p>\n\n\n\n<p class=\"has-large-font-size\"><strong>Attributes of @RequestMapping Annotation<\/strong><\/p>\n\n\n\n<p>When using the <strong>@RequestMapping<\/strong> annotation at method-level in Spring Boot application, the annotation provides a number of attributes that can be used to customize the behavior of request mapping. Some of the commonly used attributes are:                                                                                                         <\/p>\n\n\n\n<p>      <strong>-><\/strong><b>valu<\/b><strong>e:<\/strong>  This attribute specifies the URL path that the request mapping should apply to. for example,                <strong>@RequestMapping(&#8220;\/contact&#8221;) <\/strong>would match any request with the URL path <strong>&#8220;\/contact&#8221;<\/strong>.                                           <\/p>\n\n\n\n<p>     <strong>->method<\/strong>: This method specifies the HTTP method that the request mapping should apply to. It can take a <strong>RequestMethod<\/strong> value such as GET, POST, PUT, DELETE, etc. For example,                   <strong>@RequestMapping (values=&#8221;\/contact&#8221;, method=RequestMethod.GET)<\/strong> would match any GET request with URL path <strong>&#8220;\/contact&#8221;<\/strong>.                                                                                                                                              <\/p>\n\n\n\n<p>    <strong> ->params:<\/strong> This attribute specifies the request parameter conditions that should be met for the request mapping to be considered a match. For example, <strong>@RequestMapping (value = &#8220;\/contact&#8221;, params = &#8220;id<\/strong><b>&#8220;)<\/b> would match any request with URL path of <strong>&#8220;\/contact&#8221;<\/strong> that has an <strong>&#8220;id&#8221;<\/strong> parameter.<\/p>\n\n\n\n<p>                                                                      <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><a href=\"https:\/\/docs.spring.io\/spring-framework\/reference\/core\/beans\/classpath-scanning.html#beans-stereotype-annotations\" target=\"_blank\" rel=\"noopener\">Click here for more detail<\/a><\/p>\n\n\n\n<p> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Stereotype Annotation Hierarchy: The stereotype annotations are generally used to create bean(instance) of the Class&#8230;.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42,1,3],"tags":[37,41,40,39,38,33,34,36,35],"_links":{"self":[{"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/posts\/251"}],"collection":[{"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/comments?post=251"}],"version-history":[{"count":42,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/posts\/251\/revisions"}],"predecessor-version":[{"id":330,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/posts\/251\/revisions\/330"}],"wp:attachment":[{"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/media?parent=251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/categories?post=251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/tags?post=251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}