Controller nie waliduje DTO i odrzuca request "Failed to convert value of type 'null' to required type 'boolean' "

Odpowiedz Nowy wątek
2018-11-19 23:45
0

Tak jak w tytule..
Mam sobie takie DTO

public class MainSearchFilterDTO {

    public final boolean ascending;

    public final boolean withPhotos;

    public final boolean withMapPoint;
    public MainSearchFilterDTO(@JsonProperty(value = "ascending") boolean ascending,
            @JsonProperty(value = "withPhotos") boolean withPhotos,
            @JsonProperty(value = "withMapPoint") boolean withMapPoint){
        this.ascending = ascending;
        this.withPhotos = withPhotos;
        this.withMapPoint = withMapPoint;
}

i taką metodę controllera

@RequestMapping(value = "/searchresults", method = POST)
    public Set<Resource<SearchResultDTO>> getFilteredSearchResults(MainSearchFilterDTO mainSearchFilterDTO) {

        return mainSearchService.getAdvertsByFilter(mainSearchFilterDTO);
    }

ze strony angulara wysyłam posta z jsonem przetworzonym z DTO

export class MainSearchFilterDTO2 {

  private _ascending: boolean;

  private _withPhotos: boolean;

  private _withMapPoint: boolean;

  set ascending(value: boolean) {
    this._ascending = value;
  }

  get withPhotos(): boolean {
    return this._withPhotos;
  }

  set withPhotos(value: boolean) {
    this._withPhotos = value;
  }

  get withMapPoint(): boolean {
    return this._withMapPoint;
  }

  set withMapPoint(value: boolean) {
    this._withMapPoint = value;
  }

  public toJSON() {
    return {
      ascending: this._ascending,
      withPhotos: this._withPhotos,
      withMapPoint: this._withMapPoint

    };
  }
}

JSON:

{  
   "ascending":true,
   "withPhotos":true,
   "withMapPoint":false
}

I spring odbija mi request:
screenshot-20181119234218.png
detailed msg:

"Failed to convert value of type 'null' to required type 'boolean'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [@com.fasterxml.jackson.annotation.JsonProperty boolean] for value 'null'; nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type"

Pytanie.. wtf? W czym jest błąd?

edytowany 1x, ostatnio: IHaveHandedInMyResignation, 2018-11-19 23:45

Pozostało 580 znaków

2018-11-20 00:04
0
    public Set<Resource<SearchResultDTO>> getFilteredSearchResults(@RequestBody MainSearchFilterDTO mainSearchFilterDTO) {

        return mainSearchService.getAdvertsByFilter(mainSearchFilterDTO);
    }

brak adnotacki RequestBody..

weźcie usuńcie wątek, bo wstyd..

A wystarczyłoby dać @RestController zamiast @Controller ;) - Shalom 2018-11-20 09:21
@RestController() @RequestMapping("/mainsearch") public class MainSearchController { a no jest adnotacja... i tak musiałem dodać RequestBody - IHaveHandedInMyResignation 2018-11-20 15:26

Pozostało 580 znaków

Odpowiedz
Liczba odpowiedzi na stronę

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0