[Spring Boot] 스프링 부트에서 자주 사용하는 어노테이션
복습
https://shins99.tistory.com/97
[Spring Boot] 스프링 부트 시작하기( 기초 설정 방법)
복습 https://shins99.tistory.com/96 [Spring Boot] 스프링 부트 기초 스프링(Spring) 이란? - 자바 기반의 웹 어플리케이션을 만들 수 있는 프레임워크 (spring.io 사이트에서 확인하면 스프링 프레임워크는 현대
shins99.tistory.com
<@> 어노테이션( anotation )
- 주로 함수 위에 붙여서 사용함
- 작은 기능이 함수에 추가됨
목적
- 사용하면 코딩량이 획기적으로 줄어듬
@Component
- 개발자가 생성한 Class를 Spring의 Bean으로 등록할 때 사용하는 Annotation
(Spring은 해당 Annotation을 보고 Spring의 Bean으로 등록)
1
2
3
4
5
6
7
|
@Component(value="myman")
public class Man {
public Man() {
System.out.println("hi");
}
}
|
cs |
@ComponentScan
- Spring Framework는 @Component, @Service, @Repository, @Controller, @Configuration 중 1개라도 등록된 클래스를 찾으면, Context에 bean으로 등록함
- @ComponentScan Annotation이 있는 클래스의 하위 Bean을 등록 될 클래스들을 스캔하여 Bean으로 등록함
@Bean
- @Bean Annotation은 개발자가 제어가 불가능한 외부 라이브러리와 같은 것들을 Bean으로 만들 때 사용
@Controller
- Spring에게 해당 Class가 Controller의 역할을 한다고 명시하기 위해 사용하는 Annotation
1
2
3
4
5
6
7
8
9
|
@Controller // 이 Class는 Controller 역할
@RequestMapping("/user") // 이 Class는 /user로 들어오는 요청을 모두 처리
public class UserController {
@RequestMapping(method = RequestMethod.GET)
public String getUser(Model model) {
// GET method, /user 요청을 처리
}
}
|
cs |
@RequestHeader
- Request의 header값을 가져올 수 있으며, 해당 Annotation을 쓴 메소드의 파라미터에 사용
1
2
3
4
5
6
7
8
|
@Controller // 이 Class는 Controller 역할
@RequestMapping("/user") // 이 Class는 /user로 들어오는 요청을 모두 처리
public class UserController {
@RequestMapping(method = RequestMethod.GET)
public String getUser(@RequestHeader(value="Accept-Language") String acceptLanguage) {
// GET method, /user 요청을 처리
}
}
|
cs |
@RequestMapping
- @RequestMapping(value=”“)와 같은 형태로 작성하며, 요청 들어온 URI의 요청과 Annotation value 값이 일치하면 해당 클래스나 메소드가 실행됨
- Controller 객체 안의 메서드와 클래스에 적용 가능하며, 아래와 같이 사용
- Class 단위에 사용하면 하위 메소드에 모두 적용됨
- 메소드에 적용되면 해당 메소드에서 지정한 방식으로 URI를 처리
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@Controller // 이 Class는 Controller 역할
@RequestMapping("/user") // 이 Class는 /user로 들어오는 요청을 모두 처리
public class UserController {
@RequestMapping(method = RequestMethod.GET)
public String getUser(Model model) {
// GET method, /user 요청을 처리
}
@RequestMapping(method = RequestMethod.POST)
public String addUser(Model model) {
// POST method, /user 요청을 처리
}
@RequestMapping(value = "/info", method = RequestMethod.GET)
public String addUser(Model model) {
// GET method, /user/info 요청을 처리
}
}
|
cs |
@RequestParam
- URL에 전달되는 파라미터를 메소드의 인자와 매칭시켜, 파라미터를 받아서 처리할 수 있는 Annotation으로 아래와 같이 사용
- Json 형식의 Body를 MessageConverter를 통해 Java 객체로 변환
1
2
3
4
5
6
7
8
9
10
11
12
|
@Controller // 이 Class는 Controller 역할을 합니다
@RequestMapping("/user") // 이 Class는 /user로 들어오는 요청을 모두 처리합니다.
public class UserController {
@RequestMapping(method = RequestMethod.GET)
public String getUser(@RequestParam String nickname, @RequestParam(name="old") String age {
// GET method, /user 요청을 처리
// https://naver.com?nickname=dog&old=10
String sub = nickname + "_" + age;
...
}
}
|
cs |
@RequestBody
- Body에 전달되는 데이터를 메소드의 인자와 매칭, 데이터를 받아서 처리할 수 있는 Annotation으로 아래와 같이 사용
- 클라이언트가 보내는 HTTP 요청 본문(JSON 및 XML 등)을 Java 오브젝트로 변환, 아래와 같이 사용.
- 클라이언트가 body에 json or xml 과 같은 형태로 형태로 값(주로 객체)를 전송하면, 해당 내용을 Java Object로 변환
1
2
3
4
5
6
7
8
9
10
11
12
|
@Controller // 이 Class는 Controller 역할을 합니다
@RequestMapping("/user") // 이 Class는 /user로 들어오는 요청을 모두 처리합니다.
public class UserController {
@RequestMapping(method = RequestMethod.POST)
public String addUser(@RequestBody User user) {
// POST method, /user 요청을 처리
String sub_name = user.name;
String sub_old = user.old;
}
}
|
cs |
@ModelAttribute
- 클라이언트가 전송하는 HTTP parameter, Body 내용을 Setter 함수를 통해 1:1로 객체에 데이터를 연결(바인딩)함
- RequestBody와 다르게 HTTP Body 내용은 multipart/form-data 형태를 요구
- @RequestBody가 json을 받는 것과 달리 @ModenAttribute 의 경우에는 json을 받아 처리할 수 없음
@ResponseBody
- @ResponseBody은 메소드에서 리턴되는 값이 View 로 출력되지 않고 HTTP Response Body에 직접 쓰여지게 됨
- return 시에 json, xml과 같은 데이터를 return
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@Controller // 이 Class는 Controller 역할을 합니다
@RequestMapping("/user") // 이 Class는 /user로 들어오는 요청을 모두 처리합니다.
public class UserController {
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String getUser(@RequestParam String nickname, @RequestParam(name="old") String age {
// GET method, /user 요청을 처리
// https://naver.com?nickname=dog&old=10
User user = new User();
user.setName(nickname);
user.setAge(age);
return user;
}
}
|
cs |
@Autowired
Spring Framework에서 Bean 객체를 주입받기 위한 방법
Bean을 주입받기 위하여 @Autowired 를 사용
Spring Framework가 Class를 보고 Type에 맞게(Type을 먼저 확인 후, 없으면 Name 확인) Bean을 주입
- @Autowired
- 생성자 (@AllArgsConstructor 사용)
- setter
@GetMapping
- RequestMapping(Method=RequestMethod.GET)과 똑같은 역할을 하며, 아래와 같이 사용
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Controller // 이 Class는 Controller 역할을 합니다
@RequestMapping("/user") // 이 Class는 /user로 들어오는 요청을 모두 처리합니다.
public class UserController {
@GetMapping("/")
public String getUser(Model model) {
// GET method, /user 요청을 처리
}
////////////////////////////////////
// 위와 아래 메소드는 동일하게 동작합니다. //
////////////////////////////////////
@RequestMapping(method = RequestMethod.GET)
public String getUser(Model model) {
// GET method, /user 요청을 처리
}
}
|
cs |
@PostMapping
- RequestMapping(Method=RequestMethod.POST)과 똑같은 역할을 하며, 아래와 같이 사용
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Controller // 이 Class는 Controller 역할을 합니다
@RequestMapping("/user") // 이 Class는 /user로 들어오는 요청을 모두 처리합니다.
public class UserController {
@RequestMapping(method = RequestMethod.POST)
public String addUser(Model model) {
// POST method, /user 요청을 처리
}
////////////////////////////////////
// 위와 아래 메소드는 동일하게 동작합니다. //
////////////////////////////////////
@PostMapping('/')
public String addUser(Model model) {
// POST method, /user 요청을 처리
}
}
|
cs |
@SpringBootTest
- Spring Boot Test에 필요한 의존성을 제공
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// DemoApplicationTests.java
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Test
void contextLoads() {
}
}
|
cs |
@Test
- JUnit에서 테스트 할 대상을 표시
Lombok의 대표적인 Annotation과 역할
- Lombok은 코드를 크게 줄여주어 가독성을 크게 높힐 수 있는 라이브러리
- 대표적인 Annotation
@Setter
- Class 모든 필드의 Setter method를 생성
@Getter
- Class 모든 필드의 Getter method를 생성
@AllArgsConstructor
- Class 모든 필드 값을 파라미터로 받는 생성자를 추가
@NoArgsConstructor
- Class 기본 생성자를 자동으로 추가
@ToString
- Class 모든 필드의 toString method를 생성