Void function with Spring returning null in Angular 6

1

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

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

The record is deleted in this function, it is working correctly, however, the backend return 204 does not return to the Angular only. I have no idea what it can 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 it 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 that of removing. I have this exact function in another project that works perfectly.

    
asked by anonymous 30.10.2018 / 13:25

1 answer

0

Have you tried to return ResponseEntity?

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

link

    
13.11.2018 / 02:54