Overview of SpringBoot, JPA, Global Exception Handling, and Spring Cloud Annotations
This article provides a concise reference of common SpringBoot and Spring Cloud annotations, JPA entity mappings, global exception handling techniques, and related code examples, helping Java backend developers quickly understand and apply these components in their projects.
1. SpringBoot / Spring Annotations
@SpringBootApplication combines @Configuration, @EnableAutoConfiguration, and @ComponentScan and is typically placed on the main class.
@Repository marks a DAO component; @Service marks a business‑logic component; @RestController combines @Controller and @ResponseBody for REST endpoints; @Controller is used for MVC controllers that return views; @Component is a generic stereotype for beans that do not fit other categories.
@ResponseBody forces the method return value to be written directly to the HTTP response body, useful for asynchronous JSON responses.
@RequestBody binds the request body (usually JSON) to a method parameter, indicating the parameter is required.
@ComponentScan scans for classes annotated with @Component, @Controller, @Service, etc., and registers them as beans.
@Configuration designates a class as a source of bean definitions, similar to an XML configuration file.
@Bean declares a method that produces a bean to be managed by the Spring container.
@EnableAutoConfiguration enables Spring Boot’s auto‑configuration based on the classpath dependencies.
@Autowired performs by‑type injection; adding required=false makes the injection optional.
@Qualifier specifies which bean to inject when multiple candidates of the same type exist.
@Resource(name="name", type="type") performs injection by name (default) and can also specify a type.
@RequestMapping maps HTTP requests to handler methods and can be applied at class or method level. Its six attributes include params, headers, value, method, consumes, and produces.
@GetMapping, @PostMapping, etc., are shortcut annotations for specific HTTP methods.
@RequestParam binds a request parameter to a method argument; @PathVariable binds a URI template variable.
public String getByMacAddress(@PathVariable("macAddress") String macAddress) {
// do something;
}2. JPA Annotations
@Entity marks a class as a JPA entity; @Table(name="...") specifies the table name (optional if it matches the entity name).
@MappedSuperclass designates a superclass whose mapping information is inherited by subclasses.
@NoRepositoryBean prevents Spring Data from instantiating a repository interface.
@Column maps a field to a column; it can be omitted when names match.
@Id denotes the primary key; @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="repair_seq") defines a sequence‑based key generation strategy.
@SequenceGenerator configures the sequence name and allocation size.
@Transient tells the ORM to ignore a field.
@Basic(fetch=FetchType.LAZY) controls lazy loading of a field.
@JsonIgnore excludes a property from JSON serialization/deserialization.
@JoinColumn(name="loginId") defines a foreign‑key column for one‑to‑one or one‑to‑many relationships.
@OneToOne, @OneToMany, @ManyToOne describe entity relationships.
3. Global Exception Handling
@ControllerAdvice (which includes @Component) enables centralized exception handling across all controllers.
@ExceptionHandler(Exception.class) marks a method to handle a specific exception type.
4. Spring Cloud Annotations
@EnableEurekaServer turns a Spring Boot application into a Eureka service registry.
@EnableDiscoveryClient makes a service discoverable by the registry.
@LoadBalanced enables client‑side load balancing for RestTemplate.
@EnableCircuitBreaker activates circuit‑breaker functionality.
@HystrixCommand(fallbackMethod="backMethod") defines a fallback method for a protected call.
@EnableConfigServer creates a configuration server.
@EnableZuulProxy enables Zuul routing.
@SpringCloudApplication aggregates @SpringBootApplication, @EnableDiscoveryClient, and @EnableCircuitBreaker for convenience.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
