0

I have a form in angular and i am calling the post method to insert the data.But I am unable to insert the data as i am getting error like this:

"JSON parse error: Cannot construct instance of `com.ashwin.springsecurityangular.model.AuditorGroup` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('1'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.ashwin.springsecurityangular.model.AuditorGroup` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('1')
 at [Source: (PushbackInputStream); line: 1, column: 17] (through reference chain: com.ashwin.springsecurityangular.model.AssignmentAudit["auditorGroup"])"

The data that is going as AssignmentAudit through angular form is :

enter image description here

The api i am calling is: AssignmentAuditController:When i am trying to print System.out.println(register.getAssignmentDate())I am getting "null" as output.

@PostMapping(value = "/assignment-audit/create")
    public AssignmentAudit postCustomer(@RequestBody AssignmentAudit register) {
        System.out.println(register.getAssignmentDate());
        /*  Selection selection=selectionRepository.getOne(request.)*/
        System.out.println("entered assignment  audit");
        AssignmentAudit _rRegister = assignmentAuditRepository.save(new AssignmentAudit(register.getAuditorGroup(),register.getAssignmentDate(),register.getSelection(),register.getMagNo()));
        return _rRegister;
    }

AssignmentAudit.java

package com.ashwin.springsecurityangular.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Table(name = "assignmentaudit")
public class AssignmentAudit implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "auditorGroupId", nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    private AuditorGroup auditorGroup;

    @Column(name = "assignmentDate")
    private String assignmentDate;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "selectionId", nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    private Selection selection;

    @Column(name="mag_no")
    private int magNo;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public AssignmentAudit(AuditorGroup auditorGroup, String assignmentDate, Selection selection, int magNo) {

        this.auditorGroup = auditorGroup;
        this.assignmentDate = assignmentDate;
        this.selection = selection;
        this.magNo = magNo;
    }

    public AuditorGroup getAuditorGroup() {
        return auditorGroup;
    }

    public void setAuditorGroup(AuditorGroup auditorGroup) {
        this.auditorGroup = auditorGroup;
    }

    public Selection getSelection() {
        return selection;
    }

    public void setSelection(Selection selection) {
        this.selection = selection;
    }

    public int getMagNo() {
        return magNo;
    }

    public void setMagNo(int magNo) {
        this.magNo = magNo;
    }

    public String getAssignmentDate() {
        return assignmentDate;
    }

    public void setAssignmentDate(String assignmentDate) {
        this.assignmentDate = assignmentDate;
    }

    public AssignmentAudit() {

    }


    @Override
    public String toString() {
        return "AssignmentAudit [id=" + id + ", auditorGroup=" + auditorGroup + ", assignmentDate=" + assignmentDate
                + "]";
    }






}

AuditGroup.java

package com.ashwin.springsecurityangular.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "auditor_group")
public class AuditorGroup implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long auditorGroupId;

    @Column(name="group_desc")
    private String groupDesc;

    @Column(name="from_date")
    private String fromDate;

    @Column(name="to_date")
    private String toDate;

    public long getAuditorGroupId() {
        return auditorGroupId;
    }

    public void setAuditorGroupId(long auditorGroupId) {
        this.auditorGroupId = auditorGroupId;
    }

    public String getGroupDesc() {
        return groupDesc;
    }

    public void setGroupDesc(String groupDesc) {
        this.groupDesc = groupDesc;
    }

    public String getFromDate() {
        return fromDate;
    }

    public void setFromDate(String fromDate) {
        this.fromDate = fromDate;
    }

    public String getToDate() {
        return toDate;
    }

    public void setToDate(String toDate) {
        this.toDate = toDate;
    }

    public AuditorGroup() {

    }

    public AuditorGroup(String groupDesc, String fromDate, String toDate) {
        this.groupDesc = groupDesc;
        this.fromDate = fromDate;
        this.toDate = toDate;
    }



}

Selection.java

package com.ashwin.springsecurityangular.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GeneratorType;

@Entity
@Table(name="selection")
public class Selection implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long selectionId;

    @Column(name="selection_date")
    private String selectionDate;

    @Column(name="selected_by")
    private String selectedBy;

    @Column(name="pan_exim_number")
    private Long panEximNumber;

    @Column(name="name")
    private String name;

    @Column(name="address")
    private String address;

    @Column(name="phone_number")
    private String phoneNumber;

    @Column(name="selection_type")
    private String selectionType;

    @Column(name="consignment_no")
    private String consignentNo;

    @Column(name="consignment_date")
    private String consignentDate;


    @Column(name="selection_period_from_date")
    private String selectionPeriodFromDate;

    @Column(name="selection_period_to_date")
    private String selectionPeriodToDate;

    @Column(name="agent_no")
    private Long agentNo;

    @Column(name="custom_office")
    private String customOffice;

    @Column(name="auditor_group")
    private boolean auditorGroup=false;


    public Long getSelectionId() {
        return selectionId;
    }

    public void setSelectionId(Long selectionId) {
        this.selectionId = selectionId;
    }

    public String getSelectionDate() {
        return selectionDate;
    }

    public void setSelectionDate(String selectionDate) {
        this.selectionDate = selectionDate;
    }

    public String getSelectedBy() {
        return selectedBy;
    }

    public void setSelectedBy(String selectedBy) {
        this.selectedBy = selectedBy;
    }

    public Long getPanEximNumber() {
        return panEximNumber;
    }

    public void setPanEximNumber(Long panEximNumber) {
        this.panEximNumber = panEximNumber;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getSelectionType() {
        return selectionType;
    }

    public void setSelectionType(String selectionType) {
        this.selectionType = selectionType;
    }

    public String getConsignentNo() {
        return consignentNo;
    }

    public void setConsignentNo(String consignentNo) {
        this.consignentNo = consignentNo;
    }

    public String getConsignentDate() {
        return consignentDate;
    }

    public void setConsignentDate(String consignentDate) {
        this.consignentDate = consignentDate;
    }


    public String getSelectionPeriodFromDate() {
        return selectionPeriodFromDate;
    }

    public void setSelectionPeriodFromDate(String selectionPeriodFromDate) {
        this.selectionPeriodFromDate = selectionPeriodFromDate;
    }

    public String getSelectionPeriodToDate() {
        return selectionPeriodToDate;
    }

    public void setSelectionPeriodToDate(String selectionPeriodToDate) {
        this.selectionPeriodToDate = selectionPeriodToDate;
    }

    public Long getAgentNo() {
        return agentNo;
    }

    public void setAgentNo(Long agentNo) {
        this.agentNo = agentNo;
    }

    public String getCustomOffice() {
        return customOffice;
    }

    public void setCustomOffice(String customOffice) {
        this.customOffice = customOffice;
    }

    public Selection() {

    }

    @Override
    public String toString() {
        return "Selection [selectionId=" + selectionId + ", selectionDate=" + selectionDate + ", selectedBy="
                + selectedBy + ", panEximNumber=" + panEximNumber + ", name=" + name + ", address=" + address
                + ", phoneNumber=" + phoneNumber + ", selectionType=" + selectionType + ", consignentNo=" + consignentNo
                + ", consignentDate=" + consignentDate + ", selectionPeriodFromDate=" + selectionPeriodFromDate
                + ", selectionPeriodToDate=" + selectionPeriodToDate + ", agentNo=" + agentNo + ", customOffice="
                + customOffice + "]";
    }

    public boolean isAuditorGroup() {
        return auditorGroup;
    }

    public void setAuditorGroup(boolean auditorGroup) {
        this.auditorGroup = auditorGroup;
    }









}

Angular Files now: assignmentaudit.ts

export class AssignmentAudit {
    id: number;
    auditorGroup: number;
    assignmentDate: string;
    selectionNumber: number;
}

assignmentAudit form in angular :

<form name="form" #f="ngForm" (ngSubmit)="f.form.valid &&  onSubmit()" novalidate class="">
          <div class="form-group row">
            <div class="col-md-6">
              <label for="auditorGroup">Auditor Group</label>
              <select class="form-control" id="auditorGroup" required [(ngModel)]="assignmentAudit.auditorGroup" name="auditorGroup"
                #auditorGroup="ngModel"
                [ngClass]="{ 'is-invalid': f.submitted && auditorGroup.invalid }"
                required
                >
                <option *ngFor="let title of auditorgroups" [value]="title.auditorGroupId">{{title.groupDesc}}</option>

              </select>
              <div *ngIf="f.submitted && auditorGroup.invalid" class="invalid-input">
                <div *ngIf="auditorGroup.errors?.required">Auditor Group is required</div>
              </div>
            </div>
            <div class="col-md-6">
              <div class="form-group">
                <label for="assignmentDate">Assignment Date</label>
                <input type="date" id="assignmentDate"  class="form-control" name="assignmentDate" placeholder="Please enter the Asignment Date" [(ngModel)]="assignmentAudit.assignmentDate" #assignmentDate="ngModel"
                [ngClass]="{ 'is-invalid': f.submitted && assignmentDate.invalid }"
                required />
                <div *ngIf="f.submitted && assignmentDate.invalid" class="invalid-input">
                  <div *ngIf="assignmentDate.errors?.required">Assignment Date is required</div>
                </div>
              </div>
            </div>
          </div>


          <button type="submit" style="margin-bottom: 50px;" class="btn btn-success pull-right">Submit</button>
        </form>

My form looks like:

enter image description here

My method to call service class from component.ts file is

this.assignmentAuditService.createRegister(this.assignmentAudit).subscribe(data => console.log(data), error => console.log(error));

assignment-audit.service class to call the api:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})

export class AssignmentAuditService {

    private baseUrl = 'http://localhost:8080/api/assignment-audit';

    constructor(private http: HttpClient) { }

    createRegister(assignmentAudit: Object): Observable<Object> {
    return this.http.post(`${this.baseUrl}` + `/create`, assignmentAudit);
  }

}
ashwin karki
  • 643
  • 5
  • 19
  • 35

2 Answers2

1

Attributes of assignmentaudit.ts

id: number;
auditorGroup: number;
assignmentDate: string;
selectionNumber: number;

does not match with the object requirements mapped in @RequestBody AssignmentAudit register it contains attributes,

private long id;
private AuditorGroup auditorGroup;
private String assignmentDate;
private Selection selection;
private int magNo;

Here auditorGroup and selection are objects but you are passing a string to auditorGroup which is invalid. Thats what the exception is.

Either you need to change assignmentaudit.ts according to AssignmentAudit.class or create new object match with request body and put it in place of @RequestBody AssignmentAudit register. It would work.

benjamin c
  • 2,278
  • 15
  • 25
0

There is the issue with data binding object auditorGroup to premitive type. You just need to change the ngModel of your select box.

<select class="form-control" id="auditorGroup" required [(ngModel)]="assignmentAudit.auditorGroup.auditorGroupId" name="auditorGroup"
                #auditorGroup="ngModel"
                [ngClass]="{ 'is-invalid': f.submitted && auditorGroup.invalid }"
                required
                >
                <option *ngFor="let title of auditorgroups" [value]="title.auditorGroupId">{{title.groupDesc}}</option>

              </select>

Note : do not forget initialize auditorGroup of assignmentAudit object otherwise you will get undefined error.

Sunil Singh
  • 11,001
  • 2
  • 27
  • 48