Home > Blockchain >  Getting a DTO with null fields from POST request
Getting a DTO with null fields from POST request

Time:03-04

I wonder why I am getting a DTO object from the frontend app (Angular) with null fields,

Front call

public saveFoo(foo: Foo): Observable<Foo> {
    return this.http.post<Foo>(API_ROOT_URL   "/foos/", foo})
}

Back handeling

@PostMapping("/foos/")
public ResponseEntity<FooDto> createFoo(@RequestBody FooDto fooDto) {
   return ResponseEntity.ok(FooService.saveFoo(FooDto));
}

I tried adding some json type headers (as suggested here Empty request body) but in vain :

{
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json'
}

CodePudding user response:

I found the issue : my front DTO fields are just wrong and dont match with the RequestBody Object type.

My IDE (IntelliJ) adds automatiquely an underscore "_" at the beginning of every field after generating the getters and setters:

export class FooClass{

  private _foo: string;

  get foo(): string {
    return this._foo;
  }

  set foo(value: string) {
    this._foo= value;
  }
}
  • Related