2017-09-03 113 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