spring boot mysql json请求

rdrgkggo  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(312)

我想通过以下格式,而张贴时间使用邮戳。那么我如何编写模型和控制器呢。我是新来的,所以请帮帮我。

{
  "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);
 }

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

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

arknldoa

arknldoa1#

在模型类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;
}

相关问题