我正在设计一个允许执行一些基本操作的小型REST
应用程序。到目前为止好,我有以下@Entity
称为Client
需要与@Entity
沿侧坚持叫Loan
:如何处理REST中的@OneToMany关系
客户:
@Entity
@Table(name = "clients")
public class Client{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "CLIENT_ID")
private Long id;
private String name;
private String surname;
private String email;
private String phone;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "client")
private Set<Loan> loans;
public Client() {}
public Client(Long id, String name, String surname, String email, String phone) {
this.id = id;
this.name = name;
this.surname = surname;
this.email = email;
this.phone = phone;
}
// getters/setters
}
贷款:
@Entity
@Table(name = "loans")
public class Loan{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CLIENT_ID")
private Client client;
@Temporal(TemporalType.DATE)
private Date startDate = new Date();
@Temporal(TemporalType.DATE)
private Date originTerm;
private Float maxPossibleAmount;
private String notes;
public Loan() {}
public Loan(Long id, Client client, Date startDate, Date originTerm, Float maxPossibleAmount, String notes) {
this.id = id;
this.client = client;
this.startDate = startDate;
this.originTerm = originTerm;
this.maxPossibleAmount = maxPossibleAmount;
this.notes = notes;
}
// getters/setters
}
注册客户通过邮递员完美地工作,但我无法理解如何注册贷款一个特定的客户。在试图这样做,PostMan
拒绝与以下消息注册新的贷款:
{ "timestamp": 1504429329213, "status": 400, "error": "Bad Request", "exception": "org.springframework.http.converter.HttpMessageNotReadableException", "message": "JSON parse error: Can not construct instance of com.reborn.xxx.backend.models.Client: no int/Int-argument constructor/factory method to deserialize from Number value (1); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.reborn.xxx.backend.models.Client: no int/Int-argument constructor/factory method to deserialize from Number value (1)\n at [Source: [email protected]; line: 3, column: 12] (through reference chain: com.reborn.xxx.backend.models.Loan[\"client\"])", "path": "/api/loans/add" }
LoanController:
@RestController
@RequestMapping(value = "/api/loans")
public class LoanController extends BaseController {
@Autowired
private LoanService loanService;
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity registerLoan(@RequestBody Loan loan) {
Loan regLoan = loanService.registerLoan(loan);
if(regLoan == null) {
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity(HttpStatus.CREATED);
}
}
任何想法如何实现这一目标?
UPDATE1:
ClientRepository:
@Repository
public interface ClientRepository extends JpaRepository<Client, Long>{
}
LoanRepository:
@Repository
public interface LoanRepository extends JpaRepository<Loan, Long> {
}
JSON添加客户(工程):
{
"id": 1,
"name": "Arthur",
"surname": "Doyle",
"phone": 777458642,
"email": "[email protected]"
}
JSON提供贷款给特定的客户端(失败):
{
"id": 1,
"client": "http://loacalhost:8080/api/clients/find/1",
"startDate": 20170902,
"originTerm": 20170902,
"maxPossibleAmount": 5555.0000,
"notes": null
}
错误消息似乎表示没有办法从唯一的ID构建客户端。 –
你可以显示代码如何持续客户端和贷款? – Ashish451
Erm,我没有想法如何链接到当前现有的客户实体 – Reborn