我正在設計一個允許執行一些基本操作的小型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