gpt4 book ai didi

mysql - spring boot mysql JSON请求

转载 作者:行者123 更新时间:2023-11-29 18:02:58 25 4
gpt4 key购买 nike

我想在使用后期映射发布时间时传递以下格式。那么我该如何编写模型和 Controller 。我是 Spring Boot 新手,所以请帮助我。

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

我的模型和 Controller

<小时/>
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;
}

关于mysql - spring boot mysql JSON请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48162628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com