2017-09-03 114 views
0

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

錯誤消息似乎表示沒有辦法從唯一的ID構建客戶端。 –

+0

你可以顯示代碼如何持續客戶端和貸款? – Ashish451

+0

Erm,我沒有想法如何鏈接到當前現有的客戶實體 – Reborn

回答

1

LoanClient有關。所以,如果你想創建一個貸款客戶端使用該有效載荷:

POST http://loacalhost:8080/api/loans 
{ 
    "originTerm": ... 
    "maxPossibleAmount": ... 
    "notes": ... 
    "client": "http://loacalhost:8080/api/clients/1" 
} 

定製控制器是不必要的。

P.S.將@JsonIgnoreProperties("loans")添加到Loan.client以避免堆棧溢出異常...

P.P.S.在@OneToMany默認情況下,fetch參數爲FetchType.LAZY,因此您可以避開它。

+0

剛剛嘗試過您的解決方案,但效果不佳。我收到同樣的錯誤,因爲在後,但現在關於字符串 – Reborn

+0

@Reborn檢查我的演示項目https://github.com/Cepr0/sdr-one-to-many添加'碩士'使用'POST http:// localhost:8080/api/masters'具有有效負載'{「{」名稱「:」master1「 }''。要添加'Slave',使用帶有有效載荷'POST http:// localhost:8080/api/slaves''0121'/master/1「 }' – Cepr0

+0

運行POST POST:// localhost:8080/api/slaves with payload {」name「:」slave1「,」master「:」http:// localhost:8080/api /發現沒有合適的HttpMessageConverter將請求主體讀入類型爲io.github.cepr0.onetomany.bidi.Slave的對象,該請求的內容類型爲文本/無格式;字符集= UTF-8「! } – Reborn