【发布时间】:2018-12-01 09:14:31
【问题描述】:
我在@OneToMany 关系中有两个实体:
@Entity
public class Contact implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
/*.................*/
@ManyToOne
@JoinColumn(name = "fk_company")
@JsonBackReference
private Company company;
}
@Entity
public class Company implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
/*.................*/
@OneToMany(cascade = {CascadeType.ALL}, mappedBy = "company", fetch = FetchType.LAZY)
@JsonManagedReference
private Set<Contact> contacts = new HashSet<Contact>();
}
我使用 json 请求 http://localhost:8080/contacts 保存了一个新联系人,效果很好
{
"email":"root@gmail.com",
"phone":"632589714",
"firstName":"root",
"lastName":"root",
"birthday":null,
"job":"root",
"company":{
"id":1
}
}
new-contact.component.html
<form class="forms-sample">
<div class="form-group row">
<label for="email" class="col-md-2 col-form-label">Email</label>
<div class="col-md-10"><input type="email" class="form-control" id="email" name="email" placeholder="Email" [(ngModel)]="contact.email"></div>
</div>
<!--....................................................-->
<div class="form-group row">
<label for="company" class="col-md-2 col-form-label">Company</label>
<div class="col-md-10">
<select class="form-control" id="company" name="company" [(ngModel)]="contact.company">
<option *ngFor="let comp of companies" [value]="comp.id">{{comp.name}}</option>
</select>
</div>
</div>
<button class="btn btn-md-12 btn-outline-primary btn-fw btn-icon-text" (click)="saveContact(contact)">
<i class="mdi mdi-file-check btn-icon-prepend"></i>
Save
</button>
<button type="button" class="btn btn-md-12 btn-outline-secondary btn-fw btn-icon-text">
<i class="mdi mdi-reload btn-icon-prepend"></i>
Reset
</button>
</form>
但是在前端,当我想使用 Angular 5 spring 执行相同的 json 请求时,会抛出以下错误:
2018-06-22 00:21:11.051 WARN 8364 --- [io-8080-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `org.vi.entities.Company` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('3'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `org.vi.entities.Company` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('3')
at [Source: (PushbackInputStream); line: 1, column: 136] (through reference chain: org.vi.entities.Contact["company"])
2018-06-22 00:21:11.051 WARN 8364 --- [io-8080-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `org.vi.entities.Company` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('3'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `org.vi.entities.Company` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('3')
at [Source: (PushbackInputStream); line: 1, column: 136] (through reference chain: org.vi.entities.Contact["company"])
2018-06-22 01:04:51.623 WARN 8364 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Thread starvation or clock leap detected (housekeeper delta=1m8s26ms528µs835ns).
有人可以帮我吗?
【问题讨论】:
标签: json hibernate spring-boot angular5