0

I have an application in Angular IO where it is a client of a Java service. I have a search where I want to pass many search criteria. Then I created a object for this.

export class CriteriosPesquisa{
    public opcaoSelecionada: string;
    public diaDe: Date;
    public diaAte: Date;
    public numNfe: string;
}

When I made my search, I pass the object in the body of my requisition Get. ( The result I will show in one grid ) Aparently my problems are because I am using body with GET.

public getNfes(criterios: CriteriosPesquisa): Observable<Array<Nfe>>{
    const myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    // const myParams = new URLSearchParams();
    // myParams.append('criterios', criterios);
    const options = new RequestOptions({ headers: myHeaders, /*search: myParams,*/ body: JSON.stringify(criterios) });

    return this._http.request(`${URL_API}nfes`, options)
      .map(response => {
        return response.json();
      });
  }

2018-05-12 23:34:19.740 WARN 1076 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.util.List br.com.minhasCompras.service.NfeService.buscarNfe(br.com.minhasCompras.eo.Criterios)

@RequestMapping(value = "/nfes")
public List<Nfe> buscarNfe(@RequestBody Criterios criterios){

    System.out.println(criterios.getOpcaoSelecionada());
    System.out.println(criterios.getNumNfe());
    System.out.println(criterios.getDiaAte());
    System.out.println(criterios.getDiaDe());

    return new ArrayList<Nfe>();
}

Do you know something abou this ? How I can pass a object for my client with GET ?

  • The server semantic for a HTTP GET with request body is to ignore it (read https://stackoverflow.com/a/983458/9758160), it could be that Spring decides to ignore the request body hence fail to read the 'required' request body. It is much better and best practices to pass request body using POST method, what hinders you from doing so? – Joshua Chan May 13 '18 at 05:05
  • Ok, I understand you. I will follow your advice. I will use POST. Thank's – Bruno Monteiro May 13 '18 at 15:45

0 Answers0