【发布时间】:2019-07-17 17:43:28
【问题描述】:
我正在尝试实现多对一关系,其中站点属于区域。如何使用 REST 来实现这一点,只需在 POST 站 json 中传递区域 ID,如图所示。
{
"name": "New York",
"code": "MGR",
"active": "YES",
"regionid": 1
}
下面是我的控制器。
@PostMapping("/station/create/")
public ResponseObject create(@Valid @RequestBody Station station){
ResponseObject responseObject = new ResponseObject();
responseObject.setSuccess(true);
responseObject.setData(stationDao.save(station));
responseObject.setMessage("Station created successfully");
return responseObject;
}
以下是我的模型。
@Entity
@Table(name = "regions")
@EntityListeners(AuditingEntityListener.class)
public class Region implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonProperty("regionID")
private Long regionID;
@JsonProperty("name")
private String name;
@JsonProperty("active")
@Enumerated(EnumType.STRING)
private YesNo active;
}
@Entity
@Table(name = "stations")
@EntityListeners(AuditingEntityListener.class)
public class Station implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonProperty("stationID")
private Long stationID;
@JsonManagedReference
@ManyToOne
@JsonProperty("regionID")
@JoinColumn(name = "regionid", referencedColumnName = "regionid")
private Region region;
@JsonProperty("name")
private String name;
@JsonProperty("code")
private String code;
@JsonProperty("active")
@Enumerated(EnumType.STRING)
private YesNo active;
}
【问题讨论】:
-
错误是什么?
-
16,770 WARN [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter](默认任务1)无法评估Jackson反序列化类型[[简单类型,com.apas.region.model类。站]]:com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法处理托管/反向引用'defaultReference':从类型[简单类型,类com.apas.region.model.Region]中找不到反向引用属性跨度>
标签: hibernate spring-mvc jackson spring-data-jpa