<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dhanush K</title>
    <description>The latest articles on DEV Community by Dhanush K (@dhan).</description>
    <link>https://dev.to/dhan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1649551%2Fda5512bc-561a-4947-91eb-e9d02617efb3.jpg</url>
      <title>DEV Community: Dhanush K</title>
      <link>https://dev.to/dhan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dhan"/>
    <language>en</language>
    <item>
      <title>Spring Boot Annotations</title>
      <dc:creator>Dhanush K</dc:creator>
      <pubDate>Mon, 28 Oct 2024 01:12:04 +0000</pubDate>
      <link>https://dev.to/dhan/spring-boot-annotations-5hk0</link>
      <guid>https://dev.to/dhan/spring-boot-annotations-5hk0</guid>
      <description>&lt;p&gt;Annotations help you write cleaner, more concise code by eliminating the need for explicit configuration in XML files.&lt;/p&gt;

&lt;p&gt;Spring Boot annotations can be used to define the behavior of classes, methods, and fields within an application. These annotations provide a wide range of functionality, including dependency injection, web application configuration, data access, and more. &lt;/p&gt;

&lt;p&gt;By using these annotations, developers can quickly set up and configure their applications, reducing the amount of boilerplate code and making the development process more efficient.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Some frequently used annotations with explanations:&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;@SpringBootApplication&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a class-level annotation. It is used to mark the main configuration class of a Spring Boot application. It is a meta-annotation.&lt;/li&gt;
&lt;li&gt;This annotation combines three other annotations: &lt;strong&gt;&lt;code&gt;@Configuration&lt;/code&gt;, &lt;code&gt;@EnableAutoConfiguration&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;@ComponentScan&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;@Configuration&lt;/code&gt;: Indicates that the class is a source of bean definitions for the application context. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@EnableAutoConfiguration&lt;/code&gt;: Enables Spring Boot's auto-configuration mechanism, which automatically configures the Spring application based on dependencies present in the classpath. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@ComponentScan&lt;/code&gt;: Tells Spring to scan the specified package and its sub-packages for components to register in the application context.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;@Component&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a class-level annotation used to indicate that a class is a Spring component. &lt;/li&gt;
&lt;li&gt;It will automatically detect and register that class as a Spring bean in the application context. &lt;/li&gt;
&lt;li&gt;This allows you to take advantage of Spring's dependency injection capabilities and easily manage the lifecycle of the bean. &lt;/li&gt;
&lt;li&gt;It is a generic stereotype annotation in Spring that serves as a parent for more specific stereotype annotations like &lt;code&gt;@Service&lt;/code&gt;, &lt;code&gt;@Repository&lt;/code&gt;, and &lt;code&gt;@Controller&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;By using @Component, you are essentially declaring a class as a Spring-managed component without specifying a more specific stereotype.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;@Autowired&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is used in Fields, Constructors and method-level to inject dependencies automatically. &lt;/li&gt;
&lt;li&gt;It helps in achieving loose coupling between components by allowing Spring to manage the dependencies between beans. &lt;/li&gt;
&lt;li&gt;It simplifies the process of wiring beans together and reduces the need for manual configuration.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;@Scope("Singleton")&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a class-level annotation, to define a bean as a singleton scoped bean. &lt;/li&gt;
&lt;li&gt;By default, Spring beans are singletons if no scope is defined, meaning that only one instance of the bean is created and shared across the application context.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;@Scope("Prototype")&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a class-level annotation. It is used to define a bean as a prototype scoped bean. &lt;/li&gt;
&lt;li&gt;This means that a new instance of the bean will be created every time it is injected or retrieved from the Spring container.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;@Primary&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a method-level annotation. &lt;/li&gt;
&lt;li&gt;It used to indicate a primary bean when multiple beans of the same type are present in the Spring application context.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;@Qualifier&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a method-level annotation. &lt;/li&gt;
&lt;li&gt;When multiple beans of same type are defined in the Spring application context, the &lt;code&gt;@Qualifier&lt;/code&gt; annotation is &lt;strong&gt;used to specify which bean to be injected into a particular dependency&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Qualifier&lt;/code&gt; helps resolve ambiguity when auto-wiring by name is not sufficient to uniquely identify the bean to be injected.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.beans.factory.annotation.Autowired&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.beans.factory.annotation.Qualifier&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Buy&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Shop&lt;/span&gt; &lt;span class="n"&gt;shop&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="c1"&gt;//public Buy(Shop shop){&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Buy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Qualifier&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"vegPizza"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Shop&lt;/span&gt; &lt;span class="n"&gt;shop&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;shop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;shop&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;buying&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;shop&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPizza&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// Other methods in the service class&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;@Configuration&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a class-level annotation. &lt;/li&gt;
&lt;li&gt;It is used to indicate that a &lt;strong&gt;class declares one or more &lt;code&gt;@Bean&lt;/code&gt; methods&lt;/strong&gt; and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.&lt;/li&gt;
&lt;li&gt;Classes annotated with &lt;code&gt;@Configuration&lt;/code&gt; can define methods annotated with &lt;code&gt;@Bean&lt;/code&gt; &lt;strong&gt;to create and configure beans&lt;/strong&gt;. These beans are &lt;strong&gt;managed by the Spring container and can be injected into other components&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Configuration&lt;/code&gt; promotes &lt;strong&gt;modularity&lt;/strong&gt; and &lt;strong&gt;encapsulation&lt;/strong&gt; by allowing you to group related bean definitions and configurations within a single class.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;@Bean&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a method-level annotation. &lt;/li&gt;
&lt;li&gt;By annotating a method with &lt;code&gt;@Bean&lt;/code&gt;, you are &lt;strong&gt;explicitly declaring that the return value of that method should be managed by the Spring container as a bean&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Bean&lt;/code&gt; gives you more control over the &lt;strong&gt;instantiation&lt;/strong&gt; and &lt;strong&gt;configuration&lt;/strong&gt; of beans compared to component scanning or annotations like &lt;code&gt;@Component&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Beans defined using &lt;code&gt;@Bean&lt;/code&gt; can be injected into other Spring components using dependency injection by auto-wiring it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Bean&lt;/code&gt; is often used when &lt;strong&gt;integrating with external libraries&lt;/strong&gt; or frameworks that require Spring beans to be created in a specific way.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Configuration&lt;/span&gt;   &lt;span class="o"&gt;%%&lt;/span&gt; &lt;span class="no"&gt;USED&lt;/span&gt; &lt;span class="no"&gt;WITH&lt;/span&gt; &lt;span class="no"&gt;CONFIGURATION&lt;/span&gt; &lt;span class="o"&gt;%%&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppConfig&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Bean&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;MyService&lt;/span&gt; &lt;span class="nf"&gt;myService&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;MyService&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="nd"&gt;@Bean&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;MyRepository&lt;/span&gt; &lt;span class="nf"&gt;myRepository&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;MyRepository&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;@Controller&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a class level annotation.&lt;/li&gt;
&lt;li&gt;This annotation marks the class as a Spring MVC controller, which means it can handle incoming &lt;strong&gt;HTTP requests&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;It is used to &lt;strong&gt;map&lt;/strong&gt; the &lt;strong&gt;data&lt;/strong&gt; or model object to &lt;strong&gt;view&lt;/strong&gt; or template and show it to the client.&lt;/li&gt;
&lt;li&gt;We can use &lt;code&gt;@ResponseBody&lt;/code&gt; annotation on individual methods to directly return data (e.g., JSON or XML) instead of a view.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;@RestController&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a specialized version of the &lt;code&gt;@Controller&lt;/code&gt; annotation. It is known as Stereotype annotation.&lt;/li&gt;
&lt;li&gt;When a method in a class annotated with &lt;code&gt;@RestController&lt;/code&gt; returns an object, Spring &lt;strong&gt;automatically converts that object into JSON&lt;/strong&gt; or XML response using message converters. &lt;/li&gt;
&lt;li&gt;This &lt;strong&gt;eliminates the need for an additional &lt;code&gt;@ResponseBody&lt;/code&gt; annotation&lt;/strong&gt; on each method.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@RestController&lt;/code&gt; is a convenience annotation that combines &lt;code&gt;@Controller&lt;/code&gt; and &lt;code&gt;@ResponseBody&lt;/code&gt;, making it easier to write RESTful web services in Spring MVC.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;@RequestMapping&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It can be used with both method level and class level of Controller class.&lt;/li&gt;
&lt;li&gt;When annotated with &lt;strong&gt;method&lt;/strong&gt;, it automatically maps the &lt;code&gt;/Endpoint&lt;/code&gt; to &lt;code&gt;AnyMethod()&lt;/code&gt; method defined under it. &lt;/li&gt;
&lt;li&gt;When annotated with &lt;strong&gt;class&lt;/strong&gt;, it acts as a common Pre-End-Point for the methods with &lt;code&gt;/Endpoints&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Eg:&lt;/strong&gt; &lt;code&gt;/api/hello&lt;/code&gt;, where &lt;code&gt;@RequestMapping("/api")&lt;/code&gt;, &lt;code&gt;/api&lt;/code&gt; is annotated with class level.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="nd"&gt;@RequestMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/api"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%%&lt;/span&gt; &lt;span class="no"&gt;CLASS&lt;/span&gt; &lt;span class="no"&gt;LEVEL&lt;/span&gt; &lt;span class="o"&gt;%%&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ApiController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="nd"&gt;@RequestMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/hello"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;RequestMethod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;GET&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%%&lt;/span&gt; &lt;span class="no"&gt;METHOD&lt;/span&gt; &lt;span class="no"&gt;LEVEL&lt;/span&gt; &lt;span class="o"&gt;%%&lt;/span&gt;
   &lt;span class="nd"&gt;@ResponseBody&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;sayHello&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hello from API!"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;@ResponseBody&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a method-level annotation. &lt;/li&gt;
&lt;li&gt;When annotated with a method, it indicates that the return value of the method should be written directly to the HTTP response body.&lt;/li&gt;
&lt;li&gt;It converts the object into JSON/XML and return data in JSON or XML formats.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;@Service&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a class-level annotation. &lt;/li&gt;
&lt;li&gt;It is used to mark a Java class as a service layer component in the Spring application context.&lt;/li&gt;
&lt;li&gt;By using the &lt;code&gt;@Service&lt;/code&gt; annotation, you can clearly separate the service layer logic from the other layers of your application, such as the controller and repository layers, promoting a clean and maintainable architecture.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;@Repository&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a class-level annotation. &lt;/li&gt;
&lt;li&gt;@Repository: Marks a class as a data access object (DAO) that interacts with a database.&lt;/li&gt;
&lt;li&gt;It is a special type of DAO (Data Access Object) that provides an abstraction layer between the application and the data source.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;@GetMapping&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a method-level annotation. &lt;/li&gt;
&lt;li&gt;It is used to map HTTP GET (&lt;strong&gt;retrieve&lt;/strong&gt;) requests to specific handler methods in a controller class. &lt;/li&gt;
&lt;li&gt;It is used to define the URL mapping for a specific controller method. &lt;/li&gt;
&lt;li&gt;It is a &lt;strong&gt;specialized version of the &lt;code&gt;@RequestMapping&lt;/code&gt; annotation&lt;/strong&gt;, which is used to map HTTP requests to controller methods.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Controller&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/users"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;   &lt;span class="o"&gt;%%&lt;/span&gt; &lt;span class="no"&gt;NO&lt;/span&gt; &lt;span class="no"&gt;NEED&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;RequestMethod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;GET&lt;/span&gt; &lt;span class="o"&gt;%%&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;listUsers&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Model&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAllUsers&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addAttribute&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"users"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"users"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;@PostMapping&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a method-level annotation. &lt;/li&gt;
&lt;li&gt;It is used to map HTTP POST requests to specific handler methods in a controller class. &lt;/li&gt;
&lt;li&gt;It is a specialized version of the @RequestMapping annotation, which is used to map HTTP requests to controller methods.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Controller&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@PostMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/users"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestBody&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;newUser&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Create the user in the service layer&lt;/span&gt;
        &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;savedUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newUser&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;CREATED&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;savedUser&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;@putMapping&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a method-level annotation. &lt;/li&gt;
&lt;li&gt;It is used to map HTTP PUT (&lt;strong&gt;update&lt;/strong&gt;) requests to specific handler methods in a controller class. &lt;/li&gt;
&lt;li&gt;It is a &lt;strong&gt;specialized version of the &lt;code&gt;@RequestMapping&lt;/code&gt; annotation&lt;/strong&gt;, which is used to map HTTP requests to controller methods.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Controller&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@PutMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/users/{userId}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;updateUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@PathVariable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"userId"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@RequestBody&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;updatedUser&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Update the user in the service layer&lt;/span&gt;
        &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;savedUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;updateUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;updatedUser&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;savedUser&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;@DeleteMapping&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a method-level annotation. &lt;/li&gt;
&lt;li&gt;It is used to map HTTP DELETE requests to specific handler methods in a controller class. &lt;/li&gt;
&lt;li&gt;It is used to define the URL mapping for a specific controller method that handles HTTP DELETE requests. &lt;/li&gt;
&lt;li&gt;It is a specialized version of the &lt;code&gt;@RequestMapping&lt;/code&gt; annotation, which is used to map HTTP requests to controller methods.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Controller&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@DeleteMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/users/{userId}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;deleteUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@PathVariable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"userId"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Delete the user in the service layer&lt;/span&gt;
        &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;deleteUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;noContent&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;@PathVariable&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a method-parameter-level annotation.  (&lt;em&gt;used in method signature-before parameters&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;It used to bind a method parameter to a URI template variable. &lt;/li&gt;
&lt;li&gt;It is used to &lt;strong&gt;extract values from the URL path&lt;/strong&gt; and use them in your controller method.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/users/{userId}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@PathVariable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"userId"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Use the userId to fetch the user from the database&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getUserById&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;@RequestParam&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a method-level annotation. &lt;/li&gt;
&lt;li&gt;It used to bind a method parameter to a request parameter. &lt;/li&gt;
&lt;li&gt;It is used to extract values from the query string (&lt;em&gt;the part of the URL after the ?&lt;/em&gt;) and use them in your controller method.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/users"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestParam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@RequestParam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"age"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Use the name and age parameters to fetch the users from the database&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getUsersByNameAndAge&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;@Query&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a method-level annotation. &lt;/li&gt;
&lt;li&gt;It is used to define &lt;strong&gt;custom queries for repository methods&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;It is used to &lt;strong&gt;execute a custom SQL or JPQL&lt;/strong&gt; (Java Persistence Query Language) &lt;strong&gt;query&lt;/strong&gt; in your &lt;strong&gt;repository methods&lt;/strong&gt;, instead of relying on the default query generation provided by Spring Data JPA.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;JpaRepository&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Query&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SELECT u FROM User u WHERE u.email = :email"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="nf"&gt;findByEmail&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Param&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;@Param&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a method parameter-level annotation. &lt;/li&gt;
&lt;li&gt;It is used in conjunction with the &lt;code&gt;@Query&lt;/code&gt; annotation to bind method parameters to query parameters. &lt;/li&gt;
&lt;li&gt;This is particularly useful when you have &lt;strong&gt;dynamic query parameters&lt;/strong&gt; that need to be passed to the query.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;JpaRepository&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Query&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SELECT u FROM User u WHERE u.email = :email AND u.name = :name"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="nf"&gt;findByEmailAndName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Param&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@Param&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;@OneToOne&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is field/property level annotation. &lt;/li&gt;
&lt;li&gt;It is used to define a one-to-one relationship between two entity classes.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;As the Spring framework continues to evolve, the use of annotations in Spring Boot is likely to become even more prevalent, making it an essential skill for any Java developer working with enterprise-level applications.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
  </channel>
</rss>
