Java 通过Spring Boot中的POST创建对父实体的引用
我目前正在尝试用Spring Boot构建一个RESTAPI,它支持客户端和作业。客户机可以有许多作业,而作业属于客户机。但是,当我通过客户端id或试图查找客户端对象保存引用客户端的作业时,会出现以下错误:Java 通过Spring Boot中的POST创建对父实体的引用,java,spring,rest,spring-boot,Java,Spring,Rest,Spring Boot,我目前正在尝试用Spring Boot构建一个RESTAPI,它支持客户端和作业。客户机可以有许多作业,而作业属于客户机。但是,当我通过客户端id或试图查找客户端对象保存引用客户端的作业时,会出现以下错误: { "timestamp" : 1479163164739, "status" : 400, "error" : "Bad Request", "exception" : "org.springframework.http.converter.HttpMessageNotRe
{
"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"
}
包括我的作业和客户端模型和控制器:
我的工作模式:
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; }
}
作业的控制器:
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);
}
}
我只想将客户id保存到作业中,这里的正确方法是什么 我猜您正在尝试使用REST调用此方法:
@RequestMapping(value="", method=RequestMethod.POST)
public ResponseEntity createNew(@RequestBody Job job) {
repository.save(job);
return new ResponseEntity(job, HttpStatus.CREATED);
}
这个例外非常清楚,它谈到:
com.fasterxml.jackson.databind.JsonMappingException
这意味着,每当您试图在请求主体内传递作业实体时,Jackson都无法将其绑定到方法控制器上的作业
参数
我的建议是尝试编写Jackson反序列化器类,它将告诉Jackson如何进行反序列化
下面是一个如何执行此操作的示例:
你能展示一下你的要求吗。因此,我为作业实体创建了一个反序列化器和序列化器,我能够获取客户机id的值,就像该示例所做的那样。我想我的问题是:从这里开始,我应该用一个(长的)client_id实例化一个新的作业对象并尝试以这种方式保存它(并且只使用client_id作为成员而不是client对象),还是应该实例化一个client对象,尝试找到传递了id的对象,然后做点什么,给客户增加一份新的工作?我选择了后者,效果不错!刚到春天,来自一个不同的世界,非常感谢你的帮助!干杯;-)没问题
com.fasterxml.jackson.databind.JsonMappingException