关联子对象json POST Spring
我使用spring引导并通过发送POST方法来解决此问题,当我有另一个对象的关联时。
控制器:
@RequestMapping(value="/save", method=RequestMethod.POST)
public @ResponseBody void save(@RequestBody Poultry poultry){
repository.save(poultry);
}
模型:
public class Poultry {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@NotEmpty
@Column(length = 100, nullable = false)
private String name;
private String token;
@ManyToOne
private Center center;
网址:http:// localhost:8080 / poultry / save方法:POST
JSON:
{"id":null, "name": "123", "token":"1234", "center":{"id":2, "name":"Test"}}
返回方法:400错误请求
{timestamp:1435236955364 status:400 error:“Bad Request”exception:“org.springframework.http.converter.HttpMessageNotReadableException”message:“Could not read JSON:Template must not null or empty!(through reference chain:br.com .inobram.cdi.web.model.Poultry [“center”]);嵌套的异常是com.fasterxml.jackson.databind.JsonMappingException:模板不能为空或空!(通过引用链:br.com.inobram.cdi .web.model.Poultry [“center”])“path:”/ poultry / save“}
如果没有对象“center”的执行方法返回200 OK
链接地址: http://www.djcxy.com/p/48523.html