SERS.JSON
Код: Выделить всё
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
...
]
< /code>
Это приложение Spring-Boot, и у меня есть соответствующие контроллеры и сервисы. < /p>
в моем пакете домена. (Адрес и компания-это встроенные классы)@Data
@AllArgsConstructor @NoArgsConstructor
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String username;
private String email;
private String phone;
private String website;
@Embedded
private Address address;
@Embedded
private Company company;
}
< /code>
Основной файл (хранение в базе данных) < /p>
typereference и ObjectMapper от Джексона < /p>
@SpringBootApplication
public class JsondbApplication {
public static void main(String[] args) {
SpringApplication.run(JsondbApplication.class, args);
}
@Bean
CommandLineRunner runner(UserService service) {
return args -> {
ObjectMapper mapper = new ObjectMapper();
TypeReference reference = new TypeReference() {};
InputStream stream = TypeReference.class.getResourceAsStream("/json/users.json");
try {
List users = mapper.readValue(stream, reference);
service.save(users);
System.out.println("Saved!");
} catch (Exception h) {
System.out.println("Unable to save! " + h.getMessage());
}
};
}
}
Я хочу сохранить его в Single Column как json , используя Spring Boot.>
Подробнее здесь: https://stackoverflow.com/questions/599 ... pring-boot