我在@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).
有人可以帮我吗?
答案 0 :(得分:1)
JSON是一种强类型的数据表示形式,其中可以清楚地指定数字,布尔值,字符串,空字符串,空对象和null。
您正在发送company.id
为string
且定义为long
的请求对象。这就是请求处理程序抱怨无法从string
参数创建公司对象的原因。它无法隐式推断string
到long
。
请求json应该如下所示:
{
"email":"root@gmail.com",
"phone":"632589714",
"firstName":"root",
"lastName":"root",
"birthday":null,
"job":"root",
"company":{
"id":1 //without quotes
}
}