π©π»βπ¦° μ΅λͺ κ²μν μ μ. νμκ°μ , λ‘κ·ΈμΈ κΈ°λ₯μ νμ μμ!
μ λͺ©
, λ΄μ©
βid
, μ λͺ©
, λ΄μ©
ν¬ν¨ βid
λ₯Ό ν΅ν΄ μν β"status": 406, "error": "Not Acceptable"
@ToString
λΆμ¬λ‘ JsonνμΌ λ°ν μ μ£Όμ κ°μ΄ λ°νλΌμ λ°μ. β‘ DTOμ @DATA
μΆκ°π‘ λΌμ°ν
@Setter
@Getter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = "postId", callSuper = false)
@Builder
@Entity
@Table(name = "tbl_post")
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "title", nullable = false)
private String title;
@Column(name = "content", nullable = false)
private String content;
}
@Data
μ Setterλ κ·Έ μλκ° λΆλͺ
νμ§ μκ³ κ°μ²΄λ₯Ό μΈμ λ μ§ λ³κ²½ν μ μλ μνκ° λμ΄μ κ°μ²΄μ μμ μ±μ΄ 보μ₯λ°κΈ° νλ€λ€.
public interface BoardRepository extends JpaRepository<Post, Long> {}
JpaRepository
save(S entity)
μ μ₯ or μ λ°μ΄νΈ
findById(ID id)
: ID κ°μΌλ‘ κ²μ
existsById
(ID id) ID κ°μ ν΄λΉνλ λ°μ΄ν°κ° μ‘΄μ¬νλμ§ μ¬λΆ
findAll()
λͺ¨λ μν°ν°λ₯Ό κ²μ
count()
μ μ₯λ μν°ν°μ μ΄ μ
deleteById(ID id)
μ§μ λ ID κ° μμ
delete(T entity)
: μ§μ λ μν°ν°λ₯Ό μμ
deleteAll()
λͺ¨λ μν°ν°λ₯Ό μμ
- Spring Data JPA νλ μμν¬μμ μ 곡νλ μΈν°νμ΄μ€, λ°μ΄ν°λ² μ΄μ€μ μνΈμμ©
- κΈ°λ³Έμ μΈ CRUD(Create, Read, Update, Delete) μμ μ κ°μννκ³ μΆμννλ κΈ°λ₯ μ 곡
Service
λ₯Ό ν΅ν΄ κ²μκΈμ μμ±νκ³ ν΄λΌμ΄μΈνΈμ μλ΅μ λ°ννλ€. @RestController
@RequiredArgsConstructor
@RequestMapping("/api/anonymous")
public class BoardController {
private final BoardService boardService;
// κ²μκΈ μμ±
@PostMapping
public ResponseEntity<PostResponseDTO> createPost(@RequestBody final Post requestPost) {
PostResponseDTO dto = boardService.savePost(requestPost);
return ResponseEntity.ok(dto);
}
final
final
λ‘ μ μΈνμ¬ μμ μ±μ λμ
λλ€. μ€μλ‘ κ°μ μ¬ν λΉνκ±°λ μμ νλ κ²μ λ°©μ§ν©λλ€.final
λ‘ μ μΈλ 맀κ°λ³μλ λ€λ₯Έ μ€λ λμμ 맀κ°λ³μλ₯Ό μμ νλ €κ³ μλν μ μμΌλ―λ‘ μ€λ λ κ° μΆ©λμ λ°©μ§ν©λλ€. 컨νΈλ‘€λ¬λ‘λΆν° λ°μ μμ²μ μ²λ¦¬νκ³ , νμν λ°μ΄ν°λ² μ΄μ€ μμ μ μννμ¬ μλ΅μ μμ±νλ μ€κ° κ³μΈ΅ μν
CrudRepository
λ₯Ό μμ λ°μ BoardRepository
μ .save()
λ₯Ό ν΅ν΄ κ²μκΈ μ 보λ₯Ό DBμ μ μ₯νλ€.PostResponseDTO
λ‘ λ³ννμ¬ κ²°κ³Όλ₯Ό λ°ννλ€.@Service
@RequiredArgsConstructor
public class BoardService {
private final BoardRepository boardRepository;
public PostResponseDTO savePost(final Post post) {
return new PostResponseDTO(boardRepository.save(post));
}
}
public PostResponseDTO(Post post) {
this.id = post.getId();
this.title = post.getTitle();
this.content = post.getContent();
}
.stream.map.collect(Collectors.toList());
// Controller
@GetMapping
public ResponseEntity<List<PostResponseDTO>> getPostAll(){
List<PostResponseDTO> allPostDto = boardService.findAllPost();
return ResponseEntity.ok(allPostDto);
}
// Service
public List<PostResponseDTO> findAllPost(){
List<Post> posts = boardRepository.findAll();
return posts.stream()
.map(post -> new PostResponseDTO(post))
.collect(Collectors.toList());
}
?id=1
쿼리λ₯Ό ν΅ν΄ λ°μ κ²½μ°@GetMapping(params = "postId")
κ³Ό @RequestParam
// Controller
@GetMapping(params = "id")
public ResponseEntity<PostResponseDTO> getPostsById(@RequestParam("id") final Long postId){
PostResponseDTO postDto = boardService.findPostById(postId);
return ResponseEntity.ok(postDto);
}
.orElseThrow(() -> new NoSuchElementException("Post not found"));
// Service
public PostResponseDTO findById(Long postId){
return new PostResponseDTO(returnPost(postId));
}
private Post returnPost(Long postId) {
return boardRepository.findById(postId)
.orElseThrow(() -> new NoSuchElementException("Post not found"));
}
/api/anonymous/1
POST
λ‘ λ§€ν @PutMapping("/{postId}")
public ResponseEntity<PostDTO> updatePost(
@PathVariable final Long postId,
@RequestBody final PostUpdateDTO updatedPost) {
log.info("update post by id - controller : id = "+postId);
log.info("update post content - controller : content = "+updatedPost.toString());
PostDTO postDto = boardService.update(postId, updatedPost);
return ResponseEntity.ok(postDto);
}
public PostDTO update(Long postId, PostUpdateDTO updatedPost) {
Post post = returnPost(postId);
post.setTitle(updatedPost.getTitle());
post.setContent(updatedPost.getContent());
return new PostDTO(post);
}
@DeleteMapping("/{postId}")
public ResponseEntity<Boolean> deletePost(@PathVariable final Long postId) {
boolean isDeleted = boardService.deletePost(postId);
return ResponseEntity.ok(isDeleted);
}
---
public boolean deletePost(Long postId) {
try {
boardRepository.deleteById(postId);
return true; // μμ μ±κ³΅ μ true λ°ν
} catch (EmptyResultDataAccessException e) {
// ν΄λΉ IDμ κ²μκΈμ΄ μ΄λ―Έ μμ λ κ²½μ°
return false; // μμ μ€ν¨ μ false λ°ν
}
}