![](/img/trans.png)
[英]trying to read and write json data into psotgres database from Spring Boot
[英]How to read JSON String in Spring data and write to a database MySql
我有JSON文件,我想将其持久化到Mysql数据库中,任何人都可以给我一些遵循spring-data的步骤来执行此操作,我的JSON看起来像这样,我创建了一个模型,其中包含3个名为Portal.java的类, Spaces.java,Indicators.java
{
"portalName":"office360",
"spaces": [
{
"spaceName": "Modèle d'Espace collaboratif",
"indicators": [
{
"indicatorName": "Created content",
"indicatorCounter": "0"
},
{
"indicatorName": "answers reader",
"indicatorCounter": "0"
}
]
},
{
"spaceName": "Espace de Travail par Defaut",
"indicators": [
{
"indicatorName": "Created content",
"indicatorCounter": "0"
},
{
"indicatorName": "answers reader",
"indicatorCounter": "0"
}
]
}
]}
如果其中包含文件和JSON类型的数据,并且在后端中应使用Json Data填充三个类,那么我建议以下几点:
读取文件后,从文件获取数据到String:String fileString = new String(Files.readAllBytes(Paths.get(“ manifest.mf”)),StandardCharsets.UTF_8);
然后将Gson库,DTO类和此行组合:Data data = new Gson()。fromJson(fileString,Data.class);
现在您将在DTO对象中填充数据
您需要在Spring中使用RestTemplate。
步骤是
Page.class
创建一个类似的存储库类
import org.springframework.web.client.RestTemplate; @Repository public class OfficeRepository{ public Page getSomething() { RestTemplate restTemplate = new RestTemplate(); Page page = restTemplate.getForObject("http://your-url-goes-here", Page.class); return page; } }
这会将您的JSON内容返回到创建的Page对象中。
这是困难的部分。 最简单的部分是将其保存到数据库。 请在spring https://spring.io/blog/2011/02/10/getting-started-with-spring-data-jpa/之前使用此指南,以保存该指南,如果您有任何疑问,请随时提出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.