Java/Spring

애너테이션

태감새 2023. 2. 5. 12:00

@RequestBody, @ResponseBody

클라이언트와 서버의 비동기 통신.
비동기 통신을 위해서는 데이터를 보낼 때 본문에 데이터를 담아서 보내야 한다. 이 본문을 바디라고 한다. 보내는 데이터의 형식은 JSON으로 한다.

@Transactional

하나의 트랜잭션으로 관리. 모두 성공하거나 모두 실패.

동기식, 비동기식

동기식 : Request를 보내면 Response 받을때까지 대기. 트랜잭션을 맞춤
비동기식 : Request를 보내고 자길할일 함. Ajax
요청만 보내고 할일하니까 일부분만 업데이트 가능. 새로고침 없이 서버에 요청

@Controller

컨트롤러임을 알려주는 애너테이션

@RestController

@Controller + @ResponseBody

데이터 받기

  • /test/{id}로 넘겨받고 싶은 경우
    '@PathVariable 타입 id' 형태로 매개변수 받음
  • /test?id=1234로 넘겨받고 싶은 경우
    '@RequestParam 타입 id' 형태로 매개변수 받음
  • 객체로 넘겨받고 싶은 경우
    '@Requestbody 클래스 변수명' 형태로 매개변수 받음
@DeleteMapping("/api/memos/{id}")  
public Long deleteMemo(@PathVariable Long id) {  
    return memoService.deleteMemo(id);  
}
@GetMapping("/form/param")  
@ResponseBody  
public String helloGetRequestParam(@RequestParam String name, @RequestParam int age) {    
    return String.format("Hello, @RequestParam.<br> name = %s, age = %d", name, age);  
}
@PostMapping("/api/memos")  
public Memo createMemo(@RequestBody MemoRequestDto requestDto) {  
    return memoService.createMemo(requestDto);  
}

'Java > Spring' 카테고리의 다른 글

SpringMVC (1) - WAS와 WebServer  (0) 2023.04.19
스프링 배치 이해하기  (0) 2023.04.18
스프링 컨테이너  (0) 2023.02.05
Spring MVC  (0) 2023.02.04
JPA 기초  (0) 2023.02.04