๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

BackEnd๐ŸŒฑ/Spring44

[Spring] nested exception is java.lang.NullPointerException ์„œ๋ก  spring boot 2.6.7์„ ์‚ฌ์šฉ ์ค‘์ด๊ณ  swagger 3.0.0 ๋ฒ„์ „์„ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ง„ํ–‰ํ•˜๋˜ ์ค‘ ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException ํ˜„์žฌ gradle์„ ์‚ฌ์šฉ์ค‘์ด๊ธฐ์— ์•„๋ž˜์™€ ๊ฐ™์ด dependencies์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. implementation 'io.springfox:springfox-boot-starter:3.0.0' maven์ผ ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. io.springfox springfox-swagger2 3.0.0 io.springfox springfox-swag.. 2022. 5. 15.
[Spring] ๊ฐ์ฒด๋ณต์‚ฌ BeanUtils.copyProperties ์„œ๋ก  ์Šคํ”„๋ง์„ ๊ณต๋ถ€ํ•˜๋‹ค ๋ณด๋ฉด ๊ฐ์ฒด์˜ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๋ณต์‚ฌํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ๋•Œ Setter ๋ฉ”์„œ๋“œ๋กœ ์ผ์ผ์ด ๋ชจ๋‘ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์€ ์ฝ”๋“œ์˜ ๊ธธ์ด์™€ ์ž‘์„ฑ ์‹œ๊ฐ„์ด ๋Š˜์–ด๋‚˜๊ณ , ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ BeanUtils.copyProperties๋ฅผ ํ†ตํ•ด ํ•œ ์ค„๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ์›ํ•˜์ง€ ์•Š๋Š” ๊ฐ’๋“ค์€ ์ถ”๋ ค๋‚ด์–ด ์›ํ•˜๋Š” ๊ฐ’๋“ค๋งŒ ๋ณต์‚ฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. BeanUtils.copyProperties copyProperties์€ Spring์—์„œ ์ œ๊ณตํ•˜๋Š” BeanUtils ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. BeanUtils.copyProperties(Object source, Object target, String... ignoreProperties) source: ์›๋ณธ ๊ฐ์ฒด targe.. 2022. 5. 15.
[Spring] properties encoding ํ•˜๊ธฐ(๊ตญ์ œํ™” ํ•œ๊ธ€๊นจ์ง) ์„œ๋ก  ์Šคํ”„๋ง ํ•™์Šต ์ค‘ ๋‹ค๊ตญ์–ด ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด ๊ตญ์ œํ™”(Internationalization) ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋‹ค๊ฐ€ ์ด์ƒํ•˜๊ฒŒ ํ•œ๊ธ€๋งŒ ์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์ƒ์ ์œผ๋กœ Bean ๋“ฑ๋ก๋„ ํ•ด์ฃผ์—ˆ๊ณ , Controller, yml ์„ค์ •, message ๋ฒˆ๋“ค๊นŒ์ง€ ์™„๋ฒฝํ•˜๊ฒŒ ๊ตฌ์„ฑํ–ˆ๋Š”๋ฐ๋„ ์˜ค์ง ํ•œ๊ธ€ ๋ถ€๋ถ„๋งŒ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. main @SpringBootApplication public class ApiPracticeApplication { public static void main(String[] args) { SpringApplication.run(ApiPracticeApplication.class, args); } @Bean public LocaleResolver localeResolver() {.. 2022. 4. 27.
[Spring] DAO์™€ DTO์˜ ์ฐจ์ด ๊ทธ๋ฆฌ๊ณ  VO ์„œ๋ก  DAO์™€ DTO์˜ ํ‚ค์›Œ๋“œ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋˜ ์ค‘ DTO์— ๋Œ€ํ•ด์„œ๋Š” ์•Œ๊ณ  ์žˆ์ง€๋งŒ DAO๋Š” ์•„๋ฌด๋ฆฌ ๋ด๋„ Repository ๊ฐ™์€๋ฐ ๊ฐ™์€ ๊ฐœ๋…์ธ์ง€ ๊ถ๊ธˆํ•ด์„œ ์ •๋ฆฌํ•ด ๋ณด๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๋ก ์„ ๋จผ์ € ๋ง์”€๋“œ๋ฆฌ๋ฉด Repository ํŒจํ‚ค์ง€๊ฐ€ DAO์ž…๋‹ˆ๋‹ค. 1. DAO(Data Access Object) DAO๋Š” ์‹ค์ œ๋กœ DB์˜ data์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ DB์— ์ ‘๊ทผํ•˜์—ฌ data๋ฅผ ์‚ฝ์ž…, ์‚ญ์ œ, ์กฐํšŒ, ์ˆ˜์ • ๋“ฑ CRUD ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. Service์™€ DB๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ณ ๋ฆฌ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. Repository package๊ฐ€ ๋ฐ”๋กœ DAO์ž…๋‹ˆ๋‹ค. @Repository @RequiredArgsConstructor public class MemberRepository { private final Enti.. 2022. 4. 14.
[Spring] @NoArgsConstructor, @RequiredArgsConstructor, @RequiredArgsConstructor ์„œ๋ก  @NoArgsConstructor, @AllArgsConstructor, @RequiredArgsConstructor ์…‹ ๋‹ค Lombok ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์˜๋ฏธ์™€ ์–ธ์ œ ์‚ฌ์šฉํ•˜๋Š”์ง€์— ๋Œ€ํ•ด ํ•œ๋ฒˆ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์–ด๋…ธํ…Œ์ด์…˜ ์˜๋ฏธ @NoArgsConstructor ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†๋Š” ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. @AllArgsConstructor ๋ชจ๋“  ํ•„๋“œ ๊ฐ’์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. @RequiredArgsConstructor final์ด๋‚˜ @NonNull์ธ ํ•„๋“œ ๊ฐ’๋งŒ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 1. @NoArgsConstructor @NoArgsConstructor์€ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†๋Š” ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋กœ ํ•œ๋ฒˆ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. i.. 2022. 3. 30.
[Spring] @RequestParam, @RequestBody, @ModelAttribute์˜ ์ฐจ์ด ์„œ๋ก  @ModelAttribute๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ parameter์— ๊ฐ’์„ ๋„ฃ์œผ๋ฉด ์ž˜ ๋ฐ›์•„์˜ค์ง€๋งŒ body์— ๊ฐ’์„ ๋‹ด์•„ ์ „์†กํ•˜๋ฉด null๊ฐ’์ด ๋“ค์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ @RequestBody๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ”๋ฅด๊ฒŒ ๊ฐ’์ด ์ž˜ ๋‹ด์•„์ง€๋Š”๋ฐ ์™œ ๊ทธ๋Ÿฐ์ง€, ๊ทธ๋ฆฌ๊ณ  @ModelAttribute์™€ @RequestBody์˜ ์ฐจ์ด๋Š” ๋ฌด์—‡์ธ์ง€์— ๋Œ€ํ•ด ๊ถ๊ธˆํ•˜์—ฌ ํ•œ๋ฒˆ ์ •๋ฆฌ๋ฅผ ํ•ด๋ณผ๊นŒ ํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ฐ›์€ ์š”์ฒญ์„ ๊ฐ์ฒด๋กœ ๋ฐ”์ธ๋”ฉํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์—๋Š” @RequestParam, @RequestBody, @ModelAttribute ์ด ์„ธ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณตํ†ต์ ์ธ ์•„๋ž˜์™€ ๊ฐ™์€ DTO๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•œ๋ฒˆ ๊ฐ๊ฐ์˜ ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import lombok.Data; @Data public class TestData { Str.. 2022. 3. 28.
[Spring] logging ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž ์„œ๋ก  ์‹ค๋ฌด์—์„œ๋Š” System.out.println()๊ณผ ๊ฐ™์€ ์‹œ์Šคํ…œ ์ฝ˜์†”์„ ์‚ฌ์šฉํ•ด์„œ ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๊ฑฐ๋‚˜ ๋””๋ฒ„๊น…ํ•˜์ง€ ์•Š๊ณ , ๋ณ„๋„์˜ logging ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋””๋ฒ„๊น…์ด๋‚˜ ํƒ€์ž„์Šคํƒฌํ”„ ๋“ฑ ์ •ํ•ด์ง„ ์–‘์‹์— ๋งž์ถ”์–ด ํ™”๋ฉด ์ƒ์ด๋‚˜ ํŒŒ์ผ ๋กœ๊ทธ๋ฅผ ๋‚จ๊ธธ ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ฒŒ์‹œ๊ธ€์€ ๋ชฉ์ฐจ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ชฉ์ฐจ 1. logging์˜ ๋Œ€ํ‘œ์ ์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•ด ์•Œ์•„๋ณธ๋‹ค. 2. Spring Boot์—์„œ logging์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณธ๋‹ค. 3. logging๊ณผ System.out.println()์„ ๋น„๊ตํ•ด ๋ณธ๋‹ค. 1. logging์˜ ์ข…๋ฅ˜ logging ๊ด€๋ จ ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ๋Œ€ํ‘œ์ ์œผ๋กœ log4j, logback, log4j2, ๊ทธ๋ฆฌ๊ณ  ๊ทธ๊ฒƒ์„ ํ†ตํ•ฉํ•ด์„œ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ œ๊ณตํ•˜๋Š” SLF4J ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ์Šต๋‹ˆ.. 2022. 3. 21.
[Spring] JUnit5์—์„œ์˜ Exception ์ฒ˜๋ฆฌ ์„œ๋ก  JUnit5์œผ๋กœ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋˜ ์™€์ค‘์— JUnit4์™€ Exception์˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๊ณผ์ •์ด ๋‹ค๋ฅธ ์ ์ด ์žˆ์–ด์„œ ์ •๋ฆฌํ•ด ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ์ค‘๋ณต ํšŒ์›์ธ์ง€๋ฅผ ๊ฒ€์ฆํ•˜๋Š” ์•„๋ž˜ ๋ฉ”์„œ๋“œ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. private void validateDuplicateMember(Member member) { List findMembers = memberRepository.findByName(member.getName()); if (!findMembers.isEmpty()) { throw new IllegalStateException("์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํšŒ์›์ž…๋‹ˆ๋‹ค."); } } ์œ„ ์ฝ”๋“œ๋Š” ์ค‘๋ณต ํšŒ์›์ผ ๋•Œ์— IllegalStateException ์˜ˆ์™ธ๋ฅผ ๋˜์ ธ์ค๋‹ˆ๋‹ค. 1. JUnit4์—์„œ์˜ Exception.. 2022. 3. 19.