Java SpringJPA-JSON应该如何格式化以及如何使用RestTemplate调用
我已使用此规范创建了一个服务:Java SpringJPA-JSON应该如何格式化以及如何使用RestTemplate调用,java,spring,jpa,Java,Spring,Jpa,我已使用此规范创建了一个服务: @RequestMapping(value = "initCustomer", method = RequestMethod.POST) public ResponseEntity<Long> create(@RequestBody CustomerForm customerForm) { 类ProbeMonitor是一个@实体,具有@嵌入ID(因为该类在主键中有多个字段) 最后,ProbeMonitorId: @Embeddable pub
@RequestMapping(value = "initCustomer", method = RequestMethod.POST)
public ResponseEntity<Long> create(@RequestBody CustomerForm customerForm) {
类ProbeMonitor
是一个@实体
,具有@嵌入ID
(因为该类在主键中有多个字段)
最后,ProbeMonitorId
:
@Embeddable
public class ProbeMonitorId implements Serializable {
private static final long serialVersionUID = 1L;
private String customer;
private String name;
private String type;
}
现在我应该向该服务发出请求(使用RestTemplate),但首先我尝试使用一个简单的REST客户机,在该客户机中发送JSON(以检查所有工作是否正常)
我以这种格式发送JSON,但我得到“无法识别的字段“id””
我已经尝试删除JSON请求中的“id”字段,WebService将被正确调用
“监视器”应如何格式化
而且。。。(这是第二个问题)我应该如何创建RestTemplate来调用它?我想我已经发现了问题: 1-
private ProbeMonitorId id代码>未在JSON作用域中声明:
@EmbeddedId
**@XmlElement**
@**JsonProperty**
public ProbeMonitorId getId() {
return id;
}
2-插入和更新未声明为忽略
@Basic
@Column(name = "Dt_Insert")
@JsonIgnore
public Date getInserted() {
return inserted;
}
3-发送正确的JSON:
{
"name": "test_name",
"hostname": "test_hosT",
"monitors": [{
"id": {"customer": "cst", "name": "aname", "type": "atype"},
"active": "1"
}
]
若要忽略id字段,应使用@JsonIgnoreProperties(ignoreUnknown=true)注释实体。若要忽略此属性,应发送监视器列表,每个监视器都有“customer,name and type”属性-
@EmbeddedId
**@XmlElement**
@**JsonProperty**
public ProbeMonitorId getId() {
return id;
}
@Basic
@Column(name = "Dt_Insert")
@JsonIgnore
public Date getInserted() {
return inserted;
}
{
"name": "test_name",
"hostname": "test_hosT",
"monitors": [{
"id": {"customer": "cst", "name": "aname", "type": "atype"},
"active": "1"
}
]