Angular 5 :: JSON parse error while saving

蹲街弑〆低调 提交于 2019-12-23 04:47:50

问题


I have two entities in @OneToMany relationship :

@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>();
}

i save a new contact using json request http://localhost:8080/contacts and it works well

{
  "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>

but in the front end when i want to execute the same json request with Angular 5 spring throw the following error :

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).

can someone help me with that?


回答1:


JSON is strongly typed data representation where Numbers, booleans, strings, empty strings, empty objects and nulls can all be clearly specified.

You are sending request object with company.id as string and its defined as long. And that's what request handler is complaining that company object can't be created from string parameter. It can't implicitly infer string to long.

Request json should look like below:

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


来源:https://stackoverflow.com/questions/50978679/angular-5-json-parse-error-while-saving

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!