未记录DTO参考类,创建后为“null”

时间:2017-06-17 03:40:21

标签: java json spring rest dto

我有一个名为RoomDetailDto的DTO课程,如下所示

public class RoomDetailDto {

String ID;

@NotEmpty
String cd;

@NotEmpty
String name;

String dscp;


RoomCategoryDto roomCategoryDto;

//setter and getter

roomCategoryDto 引用另一个名为RoomCategoryDto的DTO,如下所示:

public class RoomCategoryDto {

String ID;

@NotEmpty
@Length(min = 0, max = 5)
String cd;

@NotEmpty
String name;

@NotEmpty
@NumberValue
String cost;

String dscp;

//setter and getter

我发送给服务器的这个JSON

  

{       “CD”: “dfs23”       “名”:“df223”       “DSCP”: “sfdsfs”       “roomCategoryDto”:{           “CD”: “KLS2”           “名字”:“Kelas 2”,           “成本”: “200000”,           “DSCP”:空,           “ID”: “7f554d7a-3c5d-4c15-921d-fa4b4c629e6c”       }   }

发送到服务器后,该记录已成功插入,但 roomCategoryDto 'NULL'

  

{       “cd”:“dfs23”,       “名字”:“df223”,       “dscp”:“sfdsfs”,       “roomCategoryDto”:null,       “id”:“ed22b38d-2bc3-4c80-b035-8936318c90c7”   }

任何人都可以建议吗? 请找到我的控制器如下:

@RequestMapping(value = "/updateAction", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public RoomDetailDto update(@Validated @RequestBody RoomDetailDto data) throws ParseException, RecordNotFoundException, java.text.ParseException {
    logger.info("add");
    RoomDetailEntity add = roomDetailService.updateRoomDetail(convertToEntity(data));
    return convertToDto(add);

}

和我的实体如下

@Entity(name = "RoomCategoryEntity") @Table(name = "t_room_category") public class RoomCategoryEntity implements Serializable {

public static final long serialVersionUID = 1L;

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "ID", unique = true)
private String ID;

@Column(name = "cd")
private String cd;

@Column(name = "name")
private String name;

@Column(name = "cost")
private String cost;

@Column(name = "dscp")
private String dscp;

@Column(name = "createDate")
private Date createDate;

@Column(name = "updateDate")
private Date updateDate;

@Column(name = "isDel")
private Date isDel; //setter and getter}

=============================================== ===================

@Entity(name = "RoomDetailEntity") @Table(name = "t_room_details") public class RoomDetailEntity implements Serializable {
public static final long serialVersionUID = 1L;

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "ID", unique = true)
private String ID;

@Column(name = "cd")
private String cd;

@Column(name = "name")
private String name;

@Column(name = "dscp")
private String dscp;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "room_category_id", referencedColumnName = "ID")
private RoomCategoryEntity roomCategoryEntity;

@Column(name = "createDate")
private Date createDate;

@Column(name = "updateDate")
private Date updateDate;

@Column(name = "isDel")
private Date isDel;

1 个答案:

答案 0 :(得分:0)

我只是尝试将引用表更改为实体,并成功插入

public class RoomDetailDto {

String ID;

@NotEmpty
String cd;

@NotEmpty
String name;

String dscp;

// I change below line
RoomCategoryEntity roomCategoryEntity;
  

[       {           “cd”:“df23”,           “名字”:“df23”,           “dscp”:“sfdsfs”,           “roomCategoryEntity”:{               “cd”:“KLS2”,               “名字”:“Kelas 2”,               “费用”:“200000”,               “dscp”:null,               “createDate”:1497535268000,               “updateDate”:null,               “isDel”:null,               “id”:“7f554d7a-3c5d-4c15-921d-fa4b4c629e6c”           },           “id”:“02c28356-9783-46a7-8ac5-9e00497d9be2”       }   ]