[Spring] REST Docs 설정
·
Spring
Spring REST Docs The aim of Spring REST Docs is to help you produce accurate and readable documentation for your RESTful services. Writing high-quality documentation is difficult. One way to ease that difficulty is to use tools that are well-suited to the job. To this end, Spring REST Docs uses Asciidoctor by default. Asciidoctor processes plain text and produces HTML, styled and laid out to sui..
[Spring Boot] com.mysql.cj.jdbc.driver not found
·
Spring
Step 0. 상황 프로젝트의 대략적인 개발 환경은 다음과 같다. 팀원 분이 설정하셨던 코드를 그대로 프로젝트를 실행해 보니 아래와 같이 찾을 수 없다는 에러가 발생했다. Java 17 Spring boot 3.0.4 Mybatis 3.0.1 dependencies { implementation 'mysql:mysql-connector-java' } Step 1. 원인 Maven Repository에 MySQL Connector를 검색하면 두 가지가 나온다. 그중 MySQL Connector Java에 들어가면 다음과 같은 문구를 확인할 수 있다. 이제는 connector-j로 지원한다는 의미인 것 같다. 그리고 실제로 Spring initializer에서 MySQL 의존성을 추가하면 아래와 같이 설정해..
[Spring] @Transactional의 Transaction Propagation
·
Spring
들어가기 전에 트랜잭션 전파란 무엇인가? 트랜잭션의 범위가 connection 기준인 것부터 이해를 해보자. The starting point of your database interaction is in making a connection. The details behind what exactly constitutes a connection vary from API to API. Nevertheless, making a connection is basically establishing some sort of link between your code and the database. 출처 : docstore.mik.ua/orelly/weblinux2/mysql/ch08_02.htm connection은 데..
@Valid exception handling (with BindException)
·
Spring
DTO 객체의 유효성을 검증하려고 @Valid 어노테이션을 사용하기로 했다. 이미 프로젝트 설정에 spring-boot-starter-validation 이 추가되어있다면 따로 설정해야 할 건 없다. 참고로 Spring Boot 2.3부터는 반드시 의존성을 추가해야 한다. PostDTO.SaveRequest public class PostDTO { @Getter @Builder public static class SaveRequest { private Long userId; @NotNull(message = "카테고리를 선택해 주세요.") private Long categoryId; @NotBlank(message = "제목을 입력해 주세요.") private String title; @NotBlank(..
MultipartFile이 있는 DTO 요청하기 (404 Bad Request 해결)
·
Spring
전에 올린 DTO와 MultipartFile을 같이 요청하는 기능을 구현한 이후에 게시물 수정 기능도 구현하다 보니 MultipartFile을 계속 파라미터로 따로 받는 것보다 DTO로 한꺼번에 받는 게 나을 것 같아서 변경하기로 결정했다. 변경하기 전 PostDTO public class PostDTO { @Getter @Builder public static class UpdateRequest { private String title; private String content; private List multipartFiles; } } Contoller @PostMapping(value = "/post") public ResponseEntity savePost(@RequestBody PostDTO.S..
DTO와 MultipartFile 요청하기 (415 Unsupported Media Type 해결)
·
Spring
게시글 작성 기능을 구현하면서 게시글 관련 데이터가 담겨있는 DTO와 MultipartFile을 같이 보냈어야 했다. Postman으로 테스트를 하고 있었는데 415 Unsupported Media Type 에러를 만나게 되면서 다음에 실수하지 않기 위해 글로 남겨두기로 했다. Controller @PostMapping("/post") public ResponseEntity savePost(@RequestBody PostDTO.saveRequest dto, @RequestPart(required = false) List images) { return ResponseEntity.ok(postService.savePost(dto, images)); } Content-Type이 각각 다른 것을 알 수 있다. ..