gpt4 book ai didi

java - 在 Spring Boot 中通过 POST 创建对实体创建的父引用

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:51:58 26 4
gpt4 key购买 nike

我目前正在尝试使用支持客户端和作业的 Spring Boot 构建 REST API。一个Client可以有多个Job,一个Job属于一个Client。但是,当我通过 client_id 或尝试查找 Client 对象保存引用 Client 的 Job 时,出现此错误:

{
"timestamp" : 1479163164739,
"status" : 400,
"error" : "Bad Request",
"exception" : "org.springframework.http.converter.HttpMessageNotReadableException",
"message" : "Could not read document: Can not construct instance of com.core.model.Client: no String-argument constructor/factory method to deserialize from String value ('3')\n at [Source: java.io.PushbackInputStream@5138cca1; line: 1, column: 12] (through reference chain: com.core.model.Job[\"client\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.core.model.Client: no String-argument constructor/factory method to deserialize from String value ('3')\n at [Source: java.io.PushbackInputStream@5138cca1; line: 1, column: 12] (through reference chain: com.core.model.Job[\"client\"])",
"path" : "/api/v1/jobs"
}

包括我的 Job 和 Client 模型和 Controller :

我的工作模型:

package com.core.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.CascadeType;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Id;

@Entity
@Table(name="`job`")
public class Job {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="job_id")
private Long jobId;

@ManyToOne(cascade=CascadeType.ALL, targetEntity = Client.class)
@JoinColumn(name = "client_id", nullable = false)
private Client client;

private String address1;
private String address2;
private String city;
private String county;
private String state;
private String zip;
private String country;
private String description;

//Note: I tried Long client_id instead of using the Client object here and same thing
public Job(String description, String address1, String address2, String city, String county,
String state, String zip, String country, Client client) {
this.setDescription(description);
this.setAddress1(address1);
this.setAddress2(address2);
...
this.setClient(client);
}

public Job() {}

public Long getId() { return jobId; }
public void setId(Long id) { this.jobId = id; }
...
public Client getClient() { return client; }
public void setClient(Client client) { this.client = client; }

}

工作 Controller :

package com.core.controller;

import com.core.model.Job;
import com.core.repository.JobRepository;
import org.apache.catalina.connector.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.core.repository.JobRepository;
import org.springframework.hateoas.Resource;

import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/api/v1/jobs")
public class JobController {

@Autowired
private JobRepository repository;

@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Collection<Job>> getAllJob(){
return new ResponseEntity<>((Collection<Job>) repository.findAll(), HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.GET, value = "/{id}")
public ResponseEntity<Job> getEquipmentWithId(@PathVariable Long id) {
return new ResponseEntity<>(repository.findOne(id),HttpStatus.OK);
}

@RequestMapping(value="", method=RequestMethod.POST)
public ResponseEntity createNew(@RequestBody Job job) {
repository.save(job);
return new ResponseEntity(job, HttpStatus.CREATED);
}
}

和客户端模型:

package com.core.model;

import javax.persistence.Entity;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
@Table(name="`client`")
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="client_id")
@Access(AccessType.PROPERTY)
private Long clientId;

public Client(String description) {
this.setDescription(description);
}

public Client() {}

public Long getClientId() {
return clientId;
}
public void setClientId(Long id) { this.clientId = id; }
...
}

我只想将该客户端 ID 保存到作业中,这里的正确方法是什么?

最佳答案

我的猜测是您正在尝试使用 REST 调用此方法:

@RequestMapping(value="", method=RequestMethod.POST)
public ResponseEntity createNew(@RequestBody Job job) {
repository.save(job);
return new ResponseEntity(job, HttpStatus.CREATED);
}

这个异常(exception)很清楚,它谈到:

com.fasterxml.jackson.databind.JsonMappingException

这意味着无论何时您尝试在请求正文中传递 Job 实体,Jackson 都无法将其绑定(bind)到方法 Controller 上的 Job job 参数。

我的建议是尝试编写 Jackson 反序列化器类,它将告诉 Jackson 究竟如何进行反序列化。

这是一个如何做到这一点的例子:

Right way to write JSON deserializer in Spring or extend it

关于java - 在 Spring Boot 中通过 POST 创建对实体创建的父引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40599387/

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