Void function with Spring returning null in Angular 6

I have the following code to just delete a record in the bank and that should return a result with the status 204-OK.

I use Spring Rest in the back and a method like void with an annotation @ResponseStatus (HttpStatus.NO_CONTENT). I tested in the Postman and it returns normal the result, and in the browser also appears the expected result. However, in the code only returns null.

The record is deleted in this function, it is working properly, but the return 204 from the backend does not return to the Angular only. I have no idea what this could be.

import { Injectable } from '@angular/core';
import { MasterHttp } from './../seguranca/master-http';
import { HttpParams } from '@angular/common/http';
import { Tipo } from './../model/Tipo';

import 'rxjs/add/operator/toPromise';
import { environment } from './../../environments/environment';

export class TipoFilter {
  tipo: string;
  pagina = 0;
  itensPorPagina = 2;
}

@Injectable({
  providedIn: 'root'
})
export class TipoService {

  tiposUrl: string;

  constructor(private http: MasterHttp) {
    this.tiposUrl = `${environment.apiUrl}/tipos`;
  }



  excluir(id: number, posicaoDaPagina: number, itensPorPagina: number): Promise<any> {
    const dadosPagina = posicaoDaPagina + itensPorPagina - 1;

    const params = new HttpParams({
       fromObject: {
          page: `${dadosPagina}`,
          size: '1'
       }
    });

    return this.http.delete<any>(`${this.tiposUrl}/${id}`)
      .toPromise()
      .then(response => {
        console.log(response);
        let proximoObjeto;
        if (response.ok) {
           proximoObjeto = this.buscarProximo(params, dadosPagina);
        } else {
           return null;
        }
        return proximoObjeto;
      });

 }

  buscarProximo(params, dadosPagina): Promise<any> {
    return this.http.get<any>(`${this.tiposUrl}`, { params })
    .toPromise()
    .then(resp => {
      const resultado = {
          lancamentos: resp.content,
          total: resp.totalElements
      };
      return (resultado.total + 1) < dadosPagina ? null : resultado;
    });
  }
}

In the backend looks like this:

@RestController
@RequestMapping("/tipos")
public class TipoResource {

    @Autowired
    private TipoRepository tipoRepository;

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('ROLE_CADASTRAR_TIPO')")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void remover(@PathVariable Long id) {
        tipoRepository.deleteById(id);
    }
}

EDIT 1 All the functions I have work on the backend normally, including the one to remove. I have this exact function in another project that works perfectly.

Author: Bruno Batista, 2018-10-30

1 answers

Have you tried returning ResponseEntity?

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('ROLE_CADASTRAR_TIPO')")
    public ResponseEntity<Void> remover(@PathVariable Long id) {
        tipoRepository.deleteById(id);
        return ResponseEntity.noContent().build();
    }

Https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html

 0
Author: Diego Biazin, 2018-11-13 01:54:02