본문 바로가기

JAVA

(54)
[JAVA] HashMap Key,Value 가져오기 1. Key, Value 모두 가져올 경우 키와 값 모두 가져와야하는 경우 entrySet() 메서드 또는 forEach() 문을 사용한다. Map.Entry 인터페이스의 entrySet() 메서드를 사용한다. Map.Entry 인터페이스는 Map 객체의 키와 값을 접근할 수 있도록 해주는 getKey(), getValue() 함수가 존재한다. Map map = new HashMap(); map.put("test", 1); map.put("sample", 2); for (Map.Entry data : map.entrySet()) { System.out.println(String.format("Key (name) is: %s, Value (age) is : %s", data.getKey(), data.ge..
[JAVA] JSON Data to JAVA Class(Object) - Json 데이터 객체(VO)로 변환(파싱) 1. jackson-databind 라이브러리 추가 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' 2. 데이터 파싱(변환) ObjectMapper mapper = new ObjectMapper(); TestVo testVo = mapper.convertValue(data, TestVo.class); json 데이터의 변수..
[JAVA] Object 값 List 여부 확인 Object 값이 List 인지 확인 하고 싶다. Object obj = new Object(); if( obj instanceof List){ }
[JAVA] Convert Object to Boolean. Object - Boolean 형변환 1. ((Boolean) Object).booleanValue(); 2. boolean bl = (Boolean) Object; 3. boolean bl = Boolean.valueOf(Object.toString())
[Spring] JAVA - formData 이용하여 여러 개의 Entity(값/객체) 전송/전달 하기 formData 로 file 뿐만 아니라 일반 Entity 객체도 여러개 전송 가능하다. 메인 Entity 에 List 로 담아서 전달하는게 편하긴 하지만 이 방법도 있다~ 하고 올려본다. Multipart/Form-data 로 객체 전송 / 전달 하기. 1. Front const Sample = async () => { const formData = new FormData(); formData.append( "info", new Blob([JSON.stringify(info)], { type: "application/json" }) ); if (list.length > 0) { list.forEach(() => formData.append( "list", new Blob([JSON.stringify(l..
[JPA] Spring JPA - deleteAll() vs deleteAllInBatch() vs @Query 사용 및 차이점 사실 실무에서는 DB 데이터 삭제는 잘 안 한다. 그래도 필요한 경우가 있긴 하다. 1. DeleteAll() /* * (non-Javadoc) * @see org.springframework.data.repository.Repository#deleteAll() */ @Override @Transactional public void deleteAll() { for (T element : findAll()) { delete(element); } } DB를 조회해서 1개씩 Delete 한다. 만약에 1000개의 데이터를 삭제해야 된다면 1000번 실행이 된다. 굉장히 비효휼적이고 낭비이다. DeleteAllInBatch() 를 사용하는 것을 추천한다. 2. DeleteAllInBatch() /* * (non..
[Spring Boot] Intellij Error - No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.0.2 was found. Intellij 에서 Project Build 시 해당 에러가 발생했다. A problem occurred configuring root project 'demo'. > Could not resolve all files for configuration ':classpath'. > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.0.2. Required by: project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.0.2 > No matching variant of org.springframework.boot:spring-boot-gradle-p..
[Swagger] Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException" SwaggerConfig에 내용 추가 후 서버를 구동하니 해당 에러가 발생했다. Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException" Spring boot 2.6 이후에 spring.mvc.pathmatch.matching-strategy 값이 ant_apth_matcher 에서 path_pattern_parser로 변경되면서 몇몇 라이브러리에 오류가 발생한다고 한다. application.properties 에 추가해주면 된다. spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER yml(yaml) 일 경우 spr..
[Spring] Spring boot - application.yml 값 변수로 사용하기 application.yml common: count: 10000 1. application.yml 혹은 properties 에 지정한 값을 가져오려면 Class명 위에 @Service 혹은 @Component 등의 어노테이션을 설정 후, @Value("&{경로}") 를 지정해주면 된다. import org.springframework.beans.factory.annotation.Value; @Service class Test { @Value("${common.count}") private int count; } 2. 별도 Config Class 파일 생성하여 지정하기. ApplicationConfig.java @Data @Configuration @EnableConfigurationProperties ..
[JPA] Spring JPA - Executing an update/delete query Update , Delete 의 경우 Transaction 처리가 필요한데 해당 처리가 없어서 발생한 에러. Executing an update/delete query 해당 메소드에 @Transactional 어노테이션 추가. import org.springframework.transaction.annotation.Transactional; @Transactional public void getTest(String testId) { testQuery.getTest(testId); }