service 패키지 생성 후 PostService 클래스 생성
package com.eom.controllerexercise.service;
import com.eom.controllerexercise.dto.PostDto;
import org.springframework.stereotype.Service;
@Service
public class PostService {
public PostDto getPost(Integer id) {
System.out.println("find post data from database by" + id);
System.out.println("validate dataa from database");
System.out.println("process data if necessary");
return new PostDto(id, "title", "this is content", "hjeom");
}
}
PostController 클래스 변경
package com.eom.controllerexercise.controller;
import com.eom.controllerexercise.dto.PostDto;
import com.eom.controllerexercise.service.PostService;
import org.springframework.web.bind.annotation.*;
import static org.springframework.web.bind.annotation.RequestMethod.*;
@RestController
@RequestMapping(value = "/post")
public class PostController {
PostService postService = new PostService();
@GetMapping
// getPost Method는 postservice를 호출해서 받은 postdto result 값을 반환을 한다.
public PostDto getPost(@RequestParam Integer id) {
PostDto result = postService.getPost(id);
return result;
}
@PostMapping
public String savePost(@RequestBody PostDto postDto) {
System.out.println(postDto.getId());
System.out.println(postDto.getTitle());
System.out.println(postDto.getContent());
System.out.println(postDto.getUsername());
return "POST /post";
}
@PutMapping
public String updatePost() {
return "PUT /post";
}
@DeleteMapping
public String deletePost() {
return "DELETE /post";
}
}
'Spring' 카테고리의 다른 글
스프링 빈과 의존성 주입 실습 (0) | 2024.03.21 |
---|---|
스프링 빈과 의존성 주입 (0) | 2024.03.21 |
Spring Service (0) | 2024.03.21 |
REST API 문서의 활용 (0) | 2024.03.20 |
HTTP Method RequestBody 실습 (0) | 2024.03.20 |