繁体   English   中英

无法保存用户:无法识别的字段类未标记为可忽略

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM