{
"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() {}
}
the main application :
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());
}
};
}
}
Unable to save users: Unrecognized field "price" (class com.shashikanth.curd.eg.entity.Product), not marked as ignorable (3 known properties: "id", "name", "quantity"]) at [Source: (BufferedInputStream); line: 7, column: 5] (through reference chain: java.util.ArrayList[0]->com.shashikanth.curd.eg.entity.Product["price"]) >
im new to springboot. im trying to map the json to an objcet and save it in mysql. it work fine without the "embedded" part(
"id": 1,
"name": "pen",
"quantity": 10,
"price": 4
}```
,)