Common Spring Boot Annotations and Their Usage

This article provides a comprehensive overview of common Spring Boot annotations, including @SpringBootApplication, @RestController, @RequestMapping, and JPA annotations, explaining their purposes, usage, and example code snippets for building Java backend applications effectively.

Java Captain
Java Captain
Java Captain
Common Spring Boot Annotations and Their Usage

@SpringBootApplication combines @ComponentScan, @Configuration and @EnableAutoConfiguration. It tells Spring Boot to automatically configure the application based on the classpath and to scan for components.

@Configuration is equivalent to an XML configuration file; it allows type‑safe Java‑based configuration.

@EnableAutoConfiguration enables Spring Boot’s auto‑configuration mechanism, which attempts to configure beans based on added dependencies.

@ComponentScan scans the package of the annotated class (and its sub‑packages) for beans annotated with @Component, @Service, @Repository, etc., and registers them in the application context.

@Component marks a class as a generic Spring component; it can be used together with CommandLineRunner to run tasks after startup.

@RestController is a shortcut for @Controller + @ResponseBody, indicating that the class handles RESTful requests and that method return values are written directly to the HTTP response body.

@ResponseBody tells Spring to write the method’s return value straight into the HTTP response body, which is useful for returning JSON from asynchronous calls.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Example of a simple controller using @RequestMapping and @ResponseBody:

@RequestMapping("/test")
@ResponseBody
public String test() {
    return "ok";
}

@Controller defines a traditional MVC controller; methods are usually mapped with @RequestMapping and return view names.

@Controller
@RequestMapping("/demoInfo")
public class DemoController {
    @Autowired
    private DemoInfoService demoInfoService;

    @RequestMapping("/hello")
    public String hello(Map<String,Object> map) {
        System.out.println("DemoController.hello()");
        map.put("hello","from DemoController.helloHtml");
        // will render hello.html or hello.ftl
        return "/hello";
    }
}

@RestController combines @Controller and @ResponseBody for REST APIs.

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demoInfo2")
public class DemoController2 {
    @RequestMapping("/test")
    public String test() {
        return "ok";
    }
}

@RequestMapping provides routing information, mapping URLs to controller methods. It supports attributes such as params, headers, method, consumes, and produces.

@RequestParam binds a request parameter to a method argument, e.g., @RequestParam String a = request.getParameter("a").

@PathVariable extracts values from the URI template.

@RequestMapping("user/get/mac/{macAddress}")
public String getByMacAddress(@PathVariable String macAddress) {
    // do something;
}

@ControllerAdvice together with @ExceptionHandler provides global exception handling for all controllers.

JPA related annotations covered include @Entity, @Table, @Id, @GeneratedValue, @Column, @OneToOne, @OneToMany, @ManyToOne, @JoinColumn, @Transient, and @JsonIgnore, each controlling how Java classes map to database tables and how they are serialized to JSON.

Additional utility annotations such as @Value, @Autowired, @Qualifier, @Resource, and @Inject are also described, showing how Spring resolves dependencies and injects configuration values.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

jpaspring-bootspring-mvc
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.