小编典典

Angular 5 ::保存时JSON解析错误

spring-boot

我在@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).

有人可以帮我吗?


阅读 273

收藏
2020-05-30

共1个答案

小编典典

JSON是一种强类型的数据表示形式,其中数字,布尔值,字符串,空字符串,空对象和null都可以明确指定。

您发送请求的对象与company.id作为string其定义为long。这就是请求处理程序抱怨无法从string参数创建公司对象的原因。它不能隐式推断stringlong

请求json应该如下所示:

{
  "email":"root@gmail.com",
  "phone":"632589714",
  "firstName":"root",
  "lastName":"root",
  "birthday":null,
  "job":"root",
  "company":{
    "id":1 //without quotes
  }
}
2020-05-30