Annotation | Description | Example |
---|---|---|
@SpringBootApplication | Marks a class as a Spring Boot application. Enables auto-configuration and component scanning. | @SpringBootApplication |
@RestController | Indicates that a class provides RESTful endpoints. It combines @Controller and @ResponseBody annotations. | @RestController |
@RequestMapping | Maps HTTP requests to handler methods of RESTful controllers. | @RequestMapping("/api") |
@Autowired | Injects dependencies into a Spring bean. It can be used for constructor, setter, or field injection. | @Autowired private MyService myService; |
@Component | Indicates that a class is a Spring-managed component. It is automatically detected during component scanning. | @Component |
@Repository | Marks a class as a Spring Data repository. It handles data access and persistence logic. | @Repository |
@Service | Marks a class as a service component in the business layer. It is used to separate business logic from presentation logic. | @Service |
@Configuration | Indicates that a class provides bean definitions. It is used along with @bean to define beans in Java-based configuration. | @Configuration |
@Value | Injects values from properties files or environment variables into Spring beans. | @Value("${my.property}") private String property; |
src
├── 메인
│ ├── 자바
│ │ └── com
│ │ └── 예시
│ │ ├── 컨트롤러
│ │ │ └── MyController.java
│ │ ├── 서비스
│ │ │ └── MyService.java
│ │ └── Application.java
│ └── 리소스
│ ├── 애플리케이션.속성
│ └── 템플릿
│ └── index.html
└── 테스트
└── 자바
└── com
└── 예시
└── 컨트롤러
└── MyControllerTest.java
오류 처리는 강력한 애플리케이션을 구축하는 데 있어 중요한 측면입니다. Spring Boot는 오류와 예외를 적절하게 처리하기 위한 여러 메커니즘을 제공하여 원활한 사용자 경험을 보장합니다.
오류 유형
@ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(MyException.class) public ResponseEntity<ErrorResponse> handleMyException(MyException ex) { ErrorResponse response = new ErrorResponse("My error message"); return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); } @ExceptionHandler(Exception.class) public ResponseEntity<ErrorResponse> handleException(Exception ex) { ErrorResponse response = new ErrorResponse("Internal server error"); return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR); } }
@ControllerAdvice public class CustomExceptionHandler extends ResponseEntityExceptionHandler { @Override protected ResponseEntity<Object> handleMethodArgumentNotValid( MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { ErrorResponse response = new ErrorResponse("Validation failed"); return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); } // Other overrides for handling specific exceptions
@Component public class CustomErrorAttributes extends DefaultErrorAttributes { @Override public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) { Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, includeStackTrace); errorAttributes.put("customAttribute", "Custom value"); return errorAttributes; } }
위 내용은 스프링 부트 치트 시트의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!