angular的HttpClient的发布请求未将正确的数据发送到服务器

时间:2020-06-11 13:00:04

标签: json angular typescript spring-boot rest

在下面的代码中,请求正在保存数据{widgetName: "widgetName", widgetCriteria: "Activities", followUpDate: "1591727400000", uid: "someId"}

let request = JSON.parse(JSON.stringify(Object.assign(this.registrationForm.value, ...req)));
      delete request.widgetFIlterOptions;
      let uid = JSON.parse(window.localStorage.getItem("user")).uid;
      request.uid = uid; 
      this.openWindow = false;
      console.info("request-->", request);
      this.contactService.addWidget(request).subscribe(res=> {
        this.emService.updateWidgits();
      })

在addWidget()函数/方法中,我们正在调用发布请求。 但在调用发布请求后,ResetController类应与其他数据一起接收“ followUpDate”。但是,在我的情况下,“ followUpDate”丢失了,但是我可以看到其他数据。

有人可以在这件事上提供帮助吗?我在这里想念的是什么?我是Angular的新手。

addWidget(widget, data?) {

    console.info("widget-->", widget); // here followUpDate is present
    this.http.post(this.api.createWidget, widget).pipe(map(data => {
        console.info("data-->", data); // this does not have the followUpDate.
        let message = "Widget created successfully";
        data['data'] = this.filterWidgets(data).length > 0 ? this.filterWidgets(data): alert("No data Available");
        this.widgets.unshift(data);
        this.toastr.success(message);
    }),catchError(err => {
      this.toastr.error(err.error);
      return throwError(err);
    }));

下面是我的休息控制器类

@RestController
public class DashboardController {

    @Autowired
    Service service;

    @PostMapping(path = "createcriteria", produces = MediaType.APPLICATION_JSON_VALUE, consumes =  MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "create the deal", response = Dashboard1.class)
    public Dashboard1 saveCriteria(@RequestBody Dashboard1 dashboard1) {

         System.out.println(dashboard1); // here "followUpDate" is missing
         return service.saveCriteria(dashboard1);
    }
}

下面是我的Dashboard1类

@Document(collection = "Dashboard1")
@JsonInclude(Include.NON_NULL)
public class Dashboard1 {

    @Id
    @ApiModelProperty(notes = "The database generated product ID")
    private String  id;

    @Field(value = "widgetname")
    private String WidgetName = null;
    @Field(value = "widgetcriteria")
    private String WidgetCriteria = null;
    @Field(value = "uid")
    private String uid = null;
    @Field(value = "activitytype")
    private String activityType = null;
    @Field(value = "contactname")
    private String contactName = null;
    @Field(value = "updateby")
    private String updateBy = null;
    @Field(value = "followUpDate")
    private String followUpDate = null;

    // below all the getters, setters and toString() methods present
}

2 个答案:

答案 0 :(得分:0)

您不需要JSON.stringfy该对象。您只需要证明json上的属性名称与spring上的类相同,就可以在不使用stringfy的情况下sed纯对象。

答案 1 :(得分:0)

我在代码中找到了问题。问题在于吸气者和安装者。在我的代码字段名称以及getter和setter名称是不同的。字段名称为index.xml,但getter和setter名称为followUpDate getFallowUpDate()