필요한 Json 파일을 구글링을 통해 구했다.

이 파일을 읽어와 java객체로 변환하여 필요한 정보인
cig_cd, gu, dong, lat, lng를 가져와 Zone 테이블에 삽입하는 과정을 진행한다.
JsonFileLoader클래스에 Gson라이브러리를 사용하여 Json파일을 읽는다.TypeToken 클래스는 목록의 유형을 List<ZoneDTO>로 지정하는 데 사용한다.ZoneDTO 클래스의 정적 메서드 toEntityList를 호출하여 ZoneDTO 개체 목록을 Zone 개체 목록으로 변환seoul.json 파일을 읽는 중 오류가 있는 경우 IOException을 throw할수있음Service에서 JsonFileLoader와 ZoneRepository를 생성자 주입 방식을 통해 의존성을 주입해 주고, jsonFileLoader.loadJsonData() 메서드를 호출하여 zoneList에 넣고 saveAll()메서드를 사용해서 persist 해 준다SaveData 메서드를 호출 해 준다.@Component
public class JsonFileLoader {
public List<Zone> loadJsonData() throws IOException {
Reader reader = new FileReader("src/main/resources/seoul.json");
Gson gson = new Gson();
List<ZoneDTO> zoneDTOList = gson.fromJson(reader, new TypeToken<List<ZoneDTO>>() {}.getType());
System.out.println("zoneDTOList: "+zoneDTOList);
return ZoneDTO.toEntityList(zoneDTOList);
}
}
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ZoneDTO {
private Long cig_cd;
private String gu;
private String dong;
private Double lat;
private Double lng;
public static List<Zone> toEntityList(List<ZoneDTO> zoneDTOList) {
return zoneDTOList.stream()
.map(zoneDTO -> Zone.builder()
.cig_cd(zoneDTO.getCig_cd())
.gu(zoneDTO.getGu())
.dong(zoneDTO.getDong())
.lat(zoneDTO.getLat())
.lng(zoneDTO.getLng())
.build())
.collect(Collectors.toList());
}
}
@Service
@Transactional
@RequiredArgsConstructor
public class ZoneService {
private final JsonFileLoader jsonFileLoader;
private final ZoneRepository zoneRepository;
public void SaveData() throws IOException {
List<Zone> zoneList = jsonFileLoader.loadJsonData();
zoneRepository.saveAll(zoneList);
}
}
@GetMapping("/save")
public void saveZone() throws IOException {
zoneService.SaveData();
}
"src/main/resources/seoul.json"이렇게 세부 경로까지 적어줘야 한다.