【问题标题】:post request of HttpClient of angular is not sending the proper data to serverAngular HttpClient 的发布请求未向服务器发送正确的数据
【发布时间】:2020-10-01 03:13:35
【问题描述】:

在我下面的代码中,请求持有数据{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() 函数/方法中,我们调用 post 请求。 但是在调用发布请求后,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
}

【问题讨论】:

  • 好吧,我们至少可以调试问题发生的位置,我希望它与反序列化有关。如果您通过 Postman 将与您在问题中发布的完全相同的 json 发送到 Java 后端,会发生什么情况?里面有followUpDate吗?
  • @IvarReukers 抱歉回复晚了...今天从邮递员那里尝试过,但仍然没有在服务器中获得“followUpDate”。

标签: json angular typescript spring-boot rest


【解决方案1】:

您不需要 JSON.stringfy 对象。您只需要证明 json 上的属性名称与您在 spring 上的类相同,并且只需 sed 没有 stringfy 的纯对象。

【讨论】:

    【解决方案2】:

    我在我的代码中发现了问题。问题在于 getter 和 setter。在我的代码字段名称中,getter 和 setter 名称不同。字段名称为followUpDate ,但getter 和setter 名称为getFallowUpDate()setFallowUpDate(String)

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2020-09-17
      • 2020-03-22
      • 2019-03-07
      • 2018-05-26
      • 2018-05-14
      • 2019-04-11
      相关资源
      最近更新 更多