๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐ˜์‘ํ˜•

๊ฐœ๋ฐœ/JAVA24

[JAVA] Error, Checked Exception, Unchecked Exception ๋น„๊ต ์ž๋ฐ”์—์„œ๋Š” ์˜ˆ์™ธ๋ฅผ ํฌ๊ฒŒ Error์™€ Exception์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๊ณ , Exception์€ RuntimeException ์ƒ์† ์—ฌ๋ถ€์— ๋”ฐ๋ผ Checked Exception, Unchecked Exception์œผ๋กœ ๊ตฌ๋ถ„๋œ๋‹ค. Error ์‹œ์Šคํ…œ์— ๋น„์ •์ƒ์ ์ธ ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ๋กœ ์‹œ์Šคํ…œ ๋ ˆ๋ฒจ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์‹ฌ๊ฐํ•œ ์ˆ˜์ค€์˜ ์˜ค๋ฅ˜๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, ๊ฐœ๋ฐœ์ž๊ฐ€ ์—์ธกํ•  ์ˆ˜ ์—†๊ธฐ์— ๋ฏธ๋ฆฌ Handlingํ•  ์ˆ˜ ์—†๋‹ค. Exception ๊ฐœ๋ฐœ์ž๊ฐ€ ๊ตฌํ˜„ํ•œ ๋กœ์ง์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋ฏธ๋ฆฌ ์˜ˆ์ธกํ•˜์—ฌ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์–ด์„œ ์ƒํ™ฉ์— ๋งž๊ฒŒ Handling ํ•ด์•ผํ•œ๋‹ค. ์•„๋ž˜๋Š” Checked Exception๊ณผ Unchecked Exception์„ ๋น„๊ตํ•œ ํ‘œ์ด๋‹ค. Checked Exception Unchecked Exception ๋กœ์ง์—์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์—ฌ.. 2022. 1. 12.
[JAVA] ์ƒ์„ฑ์ž ๋Œ€์‹  ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•˜๋Š” ์ด์œ ! ๋จผ์ € ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๊ฐ€ ๋ญ”์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์œผ๋ฉด ์•„๋ž˜ ํฌ์ŠคํŒ…์„ ๋ณด๊ณ  ์˜ค์ž! [Java] ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ž€? (static factory method) ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ ์ •์ (static), ํŒฉํ† ๋ฆฌ(factory), ๋ฉ”์„œ๋“œ(method) GoF๋ผ๋Š” ๋””์ž์ธ ํŒจํ„ด ์ค‘ ํŒฉํ† ๋ฆฌ ํŒจํ„ด์—์„œ ์œ ๋ž˜ํ•œ ๋‹จ์–ด๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์—ญํ• ์„ ๋ถ„๋ฆฌํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. (ํ•˜์ง€๋งŒ ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ” ynzu-dev.tistory.com 1. ์ด๋ฆ„์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. BigInteger ํด๋ž˜์Šค๋ฅผ ์˜ˆ๋ฅผ ๋“ค์–ด ๋ณด๋ฉด ์ƒ์„ฑ์ž์ธ BigInteger(int, int, Random)๊ณผ ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ์ธ probablePrime(int, Random) ์ค‘ ํ›„์ž๊ฐ€ ๋ฉ”์†Œ๋“œ ๋ช…๋•๋ถ„์— '๊ฐ’์ด ์†Œ์ˆ˜์ธ BigInteger๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค'๋ผ๋Š” ์˜๋ฏธ๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ ์‰ฝ๋‹ค.. 2022. 1. 12.
[JAVA] ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ž€? (static factory method) ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ ์ •์ (static), ํŒฉํ† ๋ฆฌ(factory), ๋ฉ”์„œ๋“œ(method) GoF๋ผ๋Š” ๋””์ž์ธ ํŒจํ„ด ์ค‘ ํŒฉํ† ๋ฆฌ ํŒจํ„ด์—์„œ ์œ ๋ž˜ํ•œ ๋‹จ์–ด๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์—ญํ• ์„ ๋ถ„๋ฆฌํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. (ํ•˜์ง€๋งŒ ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ์™€ ํŒฉํ† ๋ฆฌ ํŒจํ„ด์€ ๋ช…ํ™•ํ•˜๊ฒŒ ๋‹ค๋ฅด๊ณ , ๋””์ž์ธ ํŒจํ„ด ์ค‘์— ์ด์™€ ์ผ์น˜ํ•˜๋Š” ํŒจํ„ด์ด ์—†๋‹ค๊ณ  ํ•œ๋‹ค. ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„  ๊ณต๋ถ€๋ฅผ ํ•ด๋ด์•ผ๊ฒ ๋‹ค..) ์ข€ ๋” ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•˜์ž๋ฉด ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๋ณ„๋„ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐ์ฒด ๋‚ด์— ์ •์  ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•ด๋†“์€ ๊ฒƒ! ์ •ํ™•ํžˆ ์–ด๋–ค ๋ฉ”์†Œ๋“œ๋ฅผ ์–˜๊ธฐํ•˜๋Š” ๊ฑด์ง€ ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ๋‹ค. java.time ํŒจํ‚ค์ง€์— ํฌํ•จ๋œ LocalDateTime ํด๋ž˜์Šค์˜ ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ดํŽด ๋ณด๋ฉด ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ of ๋ฉ”์†Œ.. 2022. 1. 10.
[JAVA] ๋‚ ์งœ ๋น„๊ต ํ•˜๊ธฐ (Date, LocalDateTime, LocalDate) 1. java.util.Date์—๋Š” ๋‚ ์งœ๋ฅผ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ๋‘๊ฐ€์ง€ ์žˆ๋‹ค. after, before ์‚ฌ์šฉ - before: ์ธ์ž๋ณด๋‹ค ๊ณผ๊ฑฐ์ผ ๋•Œ true - after: ์ธ์ž๋ณด๋‹ค ๋ฏธ๋ž˜์ผ ๋•Œ true - equal: ์ธ์ž์™€ ๊ฐ™์„ ๋•Œ true public void compareDate(Date date1, Date date2){ if(date1.after(date2)){ System.out.println("Date1 > Date2"); } if(date1.before(date2)){ System.out.println("Date1 < Date2"); } if(date1.equals(date2)){ System.out.println("Date1 = Date2"); } } compareTo ์‚ฌ์šฉ - comp.. 2021. 12. 21.
[JAVA] VO๋ฅผ JSON ๋ฐ์ดํ„ฐ๋กœ ๋ณ€ํ™˜ (java class to json, vo to json) ๋จผ์ € ์•„๋ž˜ ํฌ์ŠคํŒ…์„ ๋ณด๊ณ  ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•˜์ž! [JAVA] JSON ๋ฐ์ดํ„ฐ๋ฅผ VO๋กœ ํŒŒ์‹ฑ, ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ• (json to java class) ๋จผ์ € jackson-databind ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค! spring boot์˜ ๊ฒฝ์šฐ 'spring-boot-starter-web'์— ํฌํ•จ๋˜์–ด ์žˆ์Œ maven com.fasterxml.jackson.core jackson-databind 2.13.0 gradle // https://mvnrepos.. ynzu-dev.tistory.com vo ์˜ˆ์‹œ public class TestVo { @JsonProperty("user-name") private String userName; @JsonProperty("user-id") private Stri.. 2021. 12. 9.
[JAVA] JSON to VO : JSON์˜ key์™€ vo(java class)์˜ ๋ณ€์ˆ˜๋ช…์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ ์•ž ํฌ์ŠคํŒ…์—์„œ json ๋ฐ์ดํ„ฐ๋ฅผ vo๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์•˜๊ณ , ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„  json์˜ key์™€ vo์˜ ๋ณ€์ˆ˜๋ช…์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ๋‹ค. json์˜ key์™€ vo์— ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋ช…์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ ๋ณ€ํ™˜์‹œ key์˜ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•˜์—ฌ ํŒŒ์‹ฑํ•˜๋ ค๋ฉด @JsonProperty์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. json data ์˜ˆ์‹œ {"user-name":"name01", "user-id":"id01"} vo ์˜ˆ์‹œ public class TestVo { @JsonProperty("user-name") private String userName; @JsonProperty("user-id") private String userId; } ์ด๋ ‡๊ฒŒ vo์˜ ๋ณ€์ˆ˜๋ช…์— JsonProperty ์–ด๋…ธํ…Œ์ด์…˜๊ณผ ํ•จ๊ป˜ ๋งคํ•‘ํ•  json d.. 2021. 12. 9.
[JAVA] JSON ๊ฐ์ฒด VO ๋ณ€ํ™˜ ์—๋Ÿฌ : org.codehaus.jackson.map.exc.UnrecognizedPropertyException json ๋ฐ์ดํ„ฐ๋ฅผ vo๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ ํ•ด๋‹น ํด๋ž˜์Šค์— ์„ ์–ธ๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜(ํ”„๋กœํผํ‹ฐ)๊ฐ€ json์— ์žˆ์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. org.codehaus.jackson.map.exc.UnrecognizedPropertyException ํ•ด๋‹น ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ฌด์‹œํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ @JsonIgnoreProperties(ignoreUnknown = true)๋ฅผ ์„ ์–ธํ•˜๋ฉด ๋œ๋‹ค! @JsonIgnoreProperties(ignoreUnknown = true) public class TestVo { private String userName; private String userId; } json ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์ด ๋“ค์–ด์™€๋„ ์ด์ œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค. {"user-name":"name01", "user-id":"id01", "u.. 2021. 12. 9.
[JAVA] JSON ๋ฐ์ดํ„ฐ๋ฅผ VO๋กœ ํŒŒ์‹ฑ, ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ• (json to java class) ๋จผ์ € jackson-databind ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค! spring boot์˜ ๊ฒฝ์šฐ 'spring-boot-starter-web'์— ํฌํ•จ๋˜์–ด ์žˆ์Œ maven com.fasterxml.jackson.core jackson-databind 2.13.0 gradle // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.0' ์œ„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ–ˆ์œผ๋ฉด ์ด ๋‘์ค„๋กœ ์‰ฝ๊ฒŒ ํŒŒ์‹ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค! ObjectMapper mapper = new ObjectM.. 2021. 12. 9.
728x90
๋ฐ˜์‘ํ˜•