我想在使用后映射发布时间时传递以下格式。所以我怎么写模型和控制器。我是春季靴子的新手,请帮帮我。

{
  "request":
  {
   "name":"siva",
   "mobile":"9788761376",
   "parent":"1",
   "description":"aaaa"
 }
}


我的模型和控制器



MODEL:
----------
@Entity
@Table(name = "project_category")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"created_date", "updated_date"},
    allowGetters = true)
public class ProjectCategoryModel {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)

private long id;

@NotBlank
private String name;


private String description;


private String parent;


@Column(nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
private Date created_date;

@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
@LastModifiedDate
private Date updated_date;

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}
public String getParent() {
    return parent;
}

public void setParent(String parent) {
    this.parent = parent;
}

public Date getCreatedDate() {
    return created_date;
}

public void setCreatedDate(Date created_date) {
    this.created_date = created_date;
}

public Date getUpdatedDate() {
    return updated_date;
}

public void setUpdatedDate(Date updated_date) {
    this.updated_date = updated_date;
}




Controller:
@PostMapping("/project/category/create")
public ResponseEntity createProjectCategory(@Valid @RequestBody
ProjectCategoryModel projectCategory) {
      String respId = "project.category.create";


    Object dbResp = projectCategoryRepository.save(projectCategory);
    ResponseDataBuilder rb = new ResponseDataBuilder();
    HashMap<String, Object> respData = new HashMap<String, Object>();
    respData.put("id",projectCategory.getId());
    respData.put("responseCode", "OK");
    respData.put("message","Project Category Created");
    respData.put("apiId","project.category.create");
    respData.put("ts", new Date(System.currentTimeMillis()));
    HashMap<String, Object> responseObj = rb.getResponseData(respId,
    respData);
    ProjectCategoryResponse response = new ProjectCategoryResponse();

    return response.sendResponse(responseObj);
 }


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

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

最佳答案

在模型类中,即ProjectCategoryModel声明一种自定义类型,例如Request

这样创建一个名为Request的类

public class Request{
  private String name;
  private String description;
  private String parent;
  private long mobile;

  //getter and setter
}


在ProjectCategoryModel中声明此类型:

MODEL:
----------
@Entity
@Table(name = "project_category")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"created_date", "updated_date"},
    allowGetters = true)
public class ProjectCategoryModel {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

private Request request;


@Column(nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
private Date created_date;

@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
@LastModifiedDate
private Date updated_date;

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public Request getRequest(){
  return request;
}

public void setRequest(Request request){
  this.request = request;
}


public Date getCreatedDate() {
    return created_date;
}

public void setCreatedDate(Date created_date) {
    this.created_date = created_date;
}

public Date getUpdatedDate() {
    return updated_date;
}

public void setUpdatedDate(Date updated_date) {
    this.updated_date = updated_date;
}

10-07 18:59
查看更多