是否可以使用 Spring Boot 将嵌套的 Json 数组存储到数据库?
Posted
技术标签:
【中文标题】是否可以使用 Spring Boot 将嵌套的 Json 数组存储到数据库?【英文标题】:Is that Possible to Store Nested Json Array To Database Using Spring Boot? 【发布时间】:2021-12-27 05:56:18 【问题描述】:这是我的 Json 格式
[
"basicInfo":
"uniqueFileNo": "123456tf",
"actualOrgid": "3",
"tRefNo": "Test Tender 1",
"tType": "Open Tender",
"tFormContract": "Works",
"tNoOfCovers": "2",
"tTenderCategory": "Works",
"tNoOfBidOpeners": "2 Off 2",
"tAllowAutomaticEvaluation": "false",
"tAllowMultiCurrencyForBoq": "false",
"creatorName": "Wils",
"creatorReference": "WilsDG"
,
"multiCurrencyForBoq": "",
"boqinfo": "",
"coverinfo":
"cover": [
"tCoverNo": "1",
"tDocDesc": "Scanned Fee Details",
"tDocType": ".pdf"
,
"tCoverNo": "2",
"tDocDesc": "UnScanned Fee Details",
"tDocType": ".csv"
]
]
我几乎完成了但是,在保存
cover
**数组结构时遇到问题** 帮我解决这个问题错误是
Cannot deserialize value of type
com.demo.model.Coverfrom Array value (token
JsonToken.START_ARRAY) at [Source: (BufferedInputStream); line: 19, column: 25] (through reference chain: java.util.ArrayList[0]->com.demo.model.TenderBasicDetails["coverinfo"]->com.demo.model.CoverInfo["cover"])
请帮我解决这个问题 谢谢
【问题讨论】:
【参考方案1】:由于您没有提供代码,我真的不知道出了什么问题,但这可能是您定义属性和/或类结构的方式:
注意:
使用 @JsonIgnoreProperties(ignoreUnknown = true)
是因为我不想定义 JSON 中的每个元素
@Data
Lombok 注解,但是你可以创建 Getter/Setters
@JsonProperty
确保 JSON 字段名映射到类字段。
应该是这样的:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Wrapper
private BasicInfo basicInfo;
private String multiCurrencyForBoq;
private String boqinfo;
private CoverInfo coverinfo;
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class BasicInfo
private String uniqueFileNo;
private String actualOrgid;
private String tRefNo;
@Data
public class CoverInfo
private List<CoverDetails> cover = new ArrayList<>();
@Data
public class CoverDetails
@JsonProperty("tCoverNo")
private String tCoverNo;
@JsonProperty("tDocDesc")
private String tDocDesc;
@JsonProperty("tDocType")
private String tDocType;
public static void main(String[] args) throws Exception
ObjectMapper objectMapper = new ObjectMapper();
String json = "[ \n" +
" \"basicInfo\": \n" +
" \"uniqueFileNo\": \"123456tf\",\n" +
" \"actualOrgid\": \"3\",\n" +
" \"tRefNo\": \"Test Tender 1\",\n" +
" \"tType\": \"Open Tender\",\n" +
" \"tFormContract\": \"Works\",\n" +
" \"tNoOfCovers\": \"2\",\n" +
" \"tTenderCategory\": \"Works\",\n" +
" \"tNoOfBidOpeners\": \"2 Off 2\",\n" +
" \"tAllowAutomaticEvaluation\": \"false\",\n" +
" \"tAllowMultiCurrencyForBoq\": \"false\",\n" +
" \"creatorName\": \"Wils\",\n" +
" \"creatorReference\": \"WilsDG\"\n" +
" ,\n" +
" \"multiCurrencyForBoq\": \"\",\n" +
" \"boqinfo\": \"\",\n" +
" \"coverinfo\": \n" +
" \"cover\": [\n" +
" \n" +
" \"tCoverNo\": \"1\",\n" +
" \"tDocDesc\": \"Scanned Fee Details\",\n" +
" \"tDocType\": \".pdf\"\n" +
" ,\n" +
" \n" +
" \"tCoverNo\": \"2\",\n" +
" \"tDocDesc\": \"UnScanned Fee Details\",\n" +
" \"tDocType\": \".csv\"\n" +
" \n" +
" ]\n" +
" \n" +
" \n" +
" ]";
System.out.println(objectMapper.readValue(json, new TypeReference<List<Wrapper>>()));
日志:
[DemoApplication.Wrapper(basicInfo=DemoApplication.BasicInfo(uniqueFileNo=123456tf, actualOrgid=3, tRefNo=null), multiCurrencyForBoq=, boqinfo=, coverinfo=DemoApplication.CoverInfo(cover=[DemoApplication.CoverDetails(tCoverNo=1, tDocDesc=Scanned Fee Details, tDocType=.pdf), DemoApplication.CoverDetails(tCoverNo=2, tDocDesc=UnScanned Fee Details, tDocType=.csv)]))]
=====已编辑============
请清理您的代码:
将@JsonIgnoreProperties(ignoreUnknown = true)
放在班级级别,然后从所有字段中删除...
如果您使用 Lombok @Data
,请删除所有 getter/setter,请
从各处删除所有@JsonProperty
....
修改您的Cover.java
以拥有:
@JsonProperty("tCoverNo")
private String tCoverNo;
@JsonProperty("tDocDesc")
private String tDocDesc;
@JsonProperty("tDocType")
private String tDocType;
@JsonProperty 背后的要点是绑定一个与 json 字段名称不同的类字段..
Class Field : blah
Json Field: BLAH
@JsonProperty("BLAH")
【讨论】:
感谢兄弟的想法,但我试试这个但是 Hibernate 不能将 Cover Entity Fields 作为表格列,所以我不知道如何解决这个问题,而且我对 stackover 流程更新鲜,所以我也不知道如何用正确的 wway 提问。对此感到抱歉,我的代码是 [github.com/akoram/Spring-Boot-Json-To-Db.git] 这里所以请看看这个以及我在这个项目中犯错的地方。 谢谢非常感谢@SMA @Akoram 更新,在我检查了你的代码之后。请相应更新并回复我。 好的兄弟@SMA以上是关于是否可以使用 Spring Boot 将嵌套的 Json 数组存储到数据库?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Spring boot 和 MYSQL 为多级菜单列表创建嵌套 JSON?
Spring Boot如何通过查找表将两个实体嵌套在一起而返回另一个实体?
Spring Boot 嵌套动态 json 请求映射到 pojo