【问题标题】:Spring REST - add Entity to DB from JSON input - DB foreign key errorSpring REST - 从 JSON 输入将实体添加到数据库 - 数据库外键错误
【发布时间】:2014-07-24 00:49:05
【问题描述】:

我有两个实体:AB

在控制器类中我有这些方法:

@RequestMapping(value="/json/get", method=RequestMethod.GET)
    public @ResponseBody List<B> getAll() {
        return BManager.findAll();
    }

@RequestMapping(value="/json/add", method=RequestMethod.POST)
    public @ResponseBody void addB(@RequestBody B b) {      
        B.setA(AManager.findByAddress(123456)); // I must be from JSON, not static!
        BManager.save(b);           
    }

网址/json/get返回给我:

{  
   "value":1,
   "title":"hello"
}

A 没有任何价值。但没关系。问题是当我想从 JSON 输入创建新的 B 实体时。

我发送

curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"value":2,"title":"world"}' http://localhost:8080/test/json/add

如果我在方法addBB.setA(AManager.findByAddress(123456)); 中将实体保存到数据库。但我需要像这样在输入 JSON 中指定地址(我可以更改 json 语法):

curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"address":123456,"value":2,"title":"world"}' http://localhost:8080/test/json/add

它崩溃了:

SEVERE: Servlet.service() for servlet [spring] in context with path [/test] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'A_id' cannot be null

问题:

1) 如何在输入 JSON 中从 B 指定地址(A 类)?

2) 如果我想要方法 getAll 的输出 JSON(B 类)中的地址(来自 A 类),我该怎么做?

我想得到/json/get:

{  
      "addrress":123456,
   "value":1,
   "title":"hello"
}

【问题讨论】:

    标签: java json spring rest jackson


    【解决方案1】:

    您的错误是由于地址类的主键在保存时为空尝试如下

    @RequestMapping(value="/json/add", method=RequestMethod.POST)
        public @ResponseBody void addB(@RequestBody B b) { 
    
            A a = AManager.findByAddress(123456);
    
            if(a == null)
            {
              // you need to create a new obj in here
    
               a = new A();
               a.setId(anyId);
                //remain code
            }
    
             //then set in here  
            B.setA(a); // I must be from JSON, not static!
            BManager.save(b);           
        }
    

    对于问题 #2,请发布您的 POJO

    更新

    看到你需要实体 B 为

     Class B {
             String id;
             String name;
             Address address;        
            //getter setters
    
            }
    
    
    
     Class Address
     {
       String address1;
       String address2;
       String code;
    
       //getter setters
     }
    

    所以在这种关系中你需要发送一个 json

    {
      name:"bla bla",
      id:"12345"
      address:{
                address1:"any dummy address",
                address2:"any dummy address",
                code:"12345"
    
      }
    
    }
    

    现在你的控制器地址对象将被spring自动映射到“b”对象

    @RequestMapping(value="/json/add", method=RequestMethod.POST)
        public @ResponseBody void addB(@RequestBody B b) { 
    
            // you can get your address object here or can save it directly
           Address address = b.getAddress();
    
            BManager.save(b);           
        }
    

    【讨论】:

    • 我需要以 JSON 格式发送地址信息。 静态“123456”仅用于测试。如果我将地址属性添加到 JSON,我如何在addB 中解析这个属性?类似ResponseBody.get("address")
    • 添加更多说明,请添加您的 pojo
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多