Mapping JSON to Entity

languages.json
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Language {

@Id
@Size(min = 3, max = 3)
private String languageCode;

private String languageName;
public Language(String languageCode) {
super();
this.languageCode = languageCode;
}
package com.countries.mehmet;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.countries.mehmet.domain.Language;
import com.countries.mehmet.service.ILanguageService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.InputStream;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
public class LanguagesCommandLineRunner implements CommandLineRunner {
private static final Logger log =
LoggerFactory.getLogger(LanguagesCommandLineRunner.class);


@Autowired
private ILanguageService languageService;

@Override
public void run(String... args) throws Exception {

loadLanguages();

}

private void loadLanguages() {
ObjectMapper mapper = new ObjectMapper();
TypeReference<List<Language>> typeReference = new TypeReference<List<Language>>(){};

InputStream inputStream = TypeReference.class.getResourceAsStream("/json/languages.json");

try {
List<Language> languages = mapper.readValue(inputStream, typeReference);
languageService.save(languages);
log.info(String.format("%d languages saved to H2 database", languages.size()));

} catch (Exception e) {
log.error("Failed to save languages to db :" + e.getMessage());
}
}
}
import java.util.Iterator;import java.util.List;import com.countries.mehmet.domain.Language;public interface ILanguageService {  List<Language> list();  Iterator<Language> save(List<Language> languages);  Language findById(String id);}

--

--

--

a geek who loves to understand the reasons behind things... and colors... Colors are cool.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Is Bluehost a Good Host ?

I want to learn how to edit my panels on twitch could I get some help plz?

Write a function to get Nth node in a Linked List

Tips to create easier Swift Playgrounds

AutoML Just Got a Lot More Automated with CD4AutoML and AWS Cloudformation Registry and CLI —…

CD4AutoML Deploye dCloudfromation resource in customer account

Demos and dealing with the black swans | Software engineering apprenticeship pattern 📚

How to waste 2020 properly?

A Siri Shortcut to Save Notes About a Location in Notion

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mehmet Akcay

Mehmet Akcay

a geek who loves to understand the reasons behind things... and colors... Colors are cool.

More from Medium

NoSuchBeanDefinitionException — 2 Ways to Resolve this Issue in SpringBoot Application

Microservices with Spring Boot

What Is Spring Boot? How and Where to use Spring Boot Annotations?

Building Microservices Application Using Spring Boot