[英]Unable to save users: Unrecognized field class not marked as ignorable
{
"id": 1,
"name": "pen",
"quantity": 10,
"price":
{
"MRP": 56,
"GST": 33
}
},
{
"id": 2,
"name": "pencil",
"quantity": 12,
"price":
{
"MRP": 34,
"GST": 12
}
},
{
"id": 3,
"name": "penpencil",
"quantity": 40,
"price":
{
"MRP": 456,
"GST": 33
}
}
]
this is the json file which is to be mapped to an object
package com.shashikanth.curd.eg.entity;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
//@NoArgsConstructor
@Entity
//@Table(name="PRODUCT_TABLE")
public class Product {
@Id
@GeneratedValue
private int id;
private String name;
private int quantity;
@Embedded
private Price price;
public Product() {}
}
package com.shashikanth.curd.eg.entity;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@Embeddable
public class Price {
@JsonIgnoreProperties(ignoreUnknown=true)
private double GST;
private double MRP;
public Price() {}
}
主要應用:
package com.shashikanth.curd.eg;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shashikanth.curd.eg.entity.Product;
import com.shashikanth.curd.eg.service.ProductService;
@SpringBootApplication
public class CurdingApplication {
public static void main(String[] args) {
SpringApplication.run(CurdingApplication.class, args);
}
@Bean
CommandLineRunner runner(ProductService productService) {
return args -> {
// read json and write to db
ObjectMapper mapper = new ObjectMapper();
// mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
TypeReference<List<Product>> typeReference = new TypeReference<List<Product>>(){};
InputStream inputStream = TypeReference.class.getResourceAsStream("/json/users.json");
try {
List<Product> products = mapper.readValue(inputStream,typeReference);
productService.saveProducts(products);
System.out.println("Users Saved!");
} catch (IOException e){
System.out.println("Unable to save users: " + e.getMessage());
}
};
}
}
無法保存用戶:在 [Source : (BufferedInputStream); 行:7,列:5](通過引用鏈:java.util.ArrayList[0]->com.shashikanth.curd.eg.entity.Product["price"])>
我是springboot的新手。 我試圖將 json 映射到一個 objcet 並將其保存在 mysql 中。 沒有“嵌入式”部分它可以正常工作(
"id": 1,
"name": "pen",
"quantity": 10,
"price": 4
}```
,)
你可以試試下面的嗎?
@Embedded @AttributeOverrides({ @AttributeOverride( name = "GST", column = @Column(name = "price_gst")), @AttributeOverride( name = "MRP", column = @Column(name = "price_mrp")) })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.