728x90 반응형 자바15 [JAVA8] null 대신 Optional 그동안 NullPointerException을 피하기 위해 어떻게 해결해 왔을까?다음은 null 확인 코드를 추가해서 NullPointerException을 줄이려는 코드입니다.// null 안전시도 1: 깊은 의심public String getCarInsuranceName(Person person) { if (person != null) { // null 확인 Car car = person.getCar(); if (car != null) { // null 확인 Insurance insurance = car.getInsurance(); if (insurance != null) { // null 확인 .. 2022. 4. 24. [Java] POI 라이브러리로 엑셀에 이미지 삽입하기 canvas로 이루어져 있는 차트 이미지를 엑셀 파일에 삽입해보겠습니다. toDataURL()을 사용하여 원하는 영역을 base64 문자열로 읽어 back단에 데이터를 전달합니다.const imageData = $("#load-graph").find("canvas")[0].toDataURL("image/png", 0.5); 만약 canvas로 그려지지 않은 영역을 이미지로 저장하고 싶다면, html2canvas.js 라이브러리를 사용하여 html 영역을 canvas로 변경하면 됩니다.html2canvas($("#chart")[0]).then(function(canvas) { const imageData = canvas.toDataURL("image/png"); ... 엑셀에 이미지 삽입하는 .. 2021. 5. 7. [Java8] 중복 데이터 제거를 위한 Stream distinct 중복 데이터를 제거하기 위해 Stream distinct를 사용합니다. 동일한 객체의 판단하는 기준은 Object.equals(Object)의 결과 값이다. String 객체의 경우 equals()가 이미 구현되어 있습니다. - Stream.distinct() : Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream. List strings = Arrays.asList("hello", "world", "hello", "java", "hello"); strings.stream().distinct().forEach(System.out::println); // "hello".. 2020. 10. 31. [java] StringBuffer getChars (Chars to String) https://www.tutorialspoint.com/java/lang/stringbuffer_getchars.htm Java.lang.StringBuffer.getChars() Method - Tutorialspoint Java.lang.StringBuffer.getChars() Method Description The java.lang.StringBuffer.getChars() method copy the characters from this sequence into the destination character array dst. The first character to be copied is at index srcBegin. The last character to www.tutorialspoin.. 2020. 10. 31. [spring] @ResponseBody Annotation vs ResponseEntity @ResponseBody annotation을 사용하면 스프링이 반환 객체를 http response body로 변환합니다. ResponseEntity는 @ResponseBody annotation과 유사하게 작동합니다. 그러나 ResponseEntity 객체를 생성할 때 response header를 http response에 추가할 수도 있습니다. responseEntity를 반환 객체로 사용하는 경우 @ResponseBody annotation을 사용할 필요가 없습니다. 예제) @ResponseBody annotation 사용 @ResponseBody @RequestMapping("/getData") public ResJsonVO getData(DataVO param) { try { return t.. 2020. 10. 31. [java] 임시 비밀번호 생성 (Random vs SecureRandom vs RandomStringUtils) java.util.Random 클래스는 난수를 생성할 때 seed값으로 시간을 이용합니다. 그래서 동일한 시간에 Random 클래스를 사용하여 난수를 사용하면 동일한 값이 리턴됩니다. 예측 가능한 난수를 사용하는 경우 공격자가 SW에서 생성되는 다음 숫자를 예상하여 시스템을 공격할 수 있습니다. 시큐어코딩 가이드 - SW 보안약점 47개 항목 중 적절하지 않은 난수 값 사용 (Use of Insufficiently Random Values)에 해당됩니다. 반면에 java.security.SecureRandom 클래스는 예측할 수 없는 seed를 이용하여 강력한 난수를 생성합니다. 임시 비밀번호 생성 getRamdomPassword(10)를 호출하면 10 글자의 임시비밀번호가 생성됩니다. import ja.. 2020. 10. 31. [springboot] REST API 적용하기 REST(Representational State Transfer) WWW과 같은 분산 하이퍼미디어 시스템을 위한 소프트웨어 아키텍처의 한 형식. 자원을 이름으로 구분하고 해당 자원의 상태를 주고받는 모든 것이 REST라고 할 수 있지만, 일반적으로 REST라고 하면 좁은 의미로 HTTP를 통해 CRUD를 실행하는 API를 뜻한다. HTTP METHOD에서 PUT과 PATCH의 차이점은 아래 포스트에서 확인하세요. [HTTP METHOD] PUT vs PATCH 차이점 HTTP 메소드 중 PUT 과 PATCH가 있다. 뭔 차이여... PUT : 자원의 전체 교체, 자원내 모든 필드 필요 (만약 전체가 아닌 일부만 전달할 경우, 전달한 필드외 모두 null or 초기값 처리되니 주의!!) PATCH : �.. 2020. 10. 14. [springboot] MyBatis resultType이 Map일경우 key를 소문자로 만들기 보통 MyBatis에서 resultType으로 목록이나 데이터를 조회할 경우 VO(Value Object)를 많이 사용합니다. 그런데 VO 가 아닌 map을 사용할 경우 key 값이 대문자로 들어갑니다. (oracle, h2 등) VO는 보통 camelCase 이거나 언더바를 포함한 소문자 명명규칙을 사용합니다. Map의 Key를 VO처럼 소문자 명명규칙을 사용하게 설정해봅시다. 사실 전자정부프레임워크를 사용하면 EgovMap을 사용하여 camelCase로 key를 세팅하도록 되어있습니다. 그래서 전자정부프레임워크처럼 Map을 상속받는 클래스를 생성하고 put 함수를 가로채 key를 lowerCase로 치환해주는 새로운 Map 객체를 만들어보겠습니다. 전자정부 프레임워크가 사용하고 있는 방법이기 때문에 .. 2020. 10. 14. [springboot] h2-console 접속이 안될 경우 문제 해결 h2-console 접속이 안될 경우 해결 방법을 알려드리겠습니다. http://localhost:8080/h2-console/ 1. h2-console enabled h2-console 접속이 안된다면 application.yml에 아래 내용을 추가합니다. spring h2: console: enabled: true 2. Spring Security + H2 잘 작동하던 H2 데이터베이스가 Spring Security를 적용하자 작동을 하지 않는 이유는 Spring Security에서 H2 데이터베이스 콘솔 접근을 차단했기 때문입니다. 다음과 같이 Java Security Configuration을 설정해줍니다. @Configuration @EnableWebSecurity public class Web.. 2020. 10. 14. [springboot] jar파일이 jsp경로를 못찾는 경우, intellij에서 war 빌드 /src/main/webapp/WEB-INF/* 경로에 jsp 파일이 있을 경우, application.yml 에 아래처럼 설정을 하고서 개발 툴에서는 잘 작동하던 springboot 프로젝트가 jar로 빌드하면 경로를 못 찾고404 에러가 뜰 때! // application.yml spring: mvc: view: prefix: /WEB-INF/view/ suffix: .jsp 원인은 jar로 된 빌드 파일은 더 이상 jsp를 지원하지 않는다고 합니다. 그래서 jar가 아닌 war로 빌드를 해야 합니다. JSP Limitations When running a Spring Boot application that uses an embedded servlet container (and is packaged .. 2020. 10. 11. 이전 1 2 다음 728x90 반응형