1
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 on the back and a void method with a @Responsestatus(Httpstatus.NO_CONTENT) annotation. I tested in 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, is working correctly, but the 204 return from the backend does not return to Angular only. I have no idea what it 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 it’s 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 in the backend normally, including the one to remove. I have this exact function in another project that works perfectly.
Try to change the return from void to Object, if it works, I publish the answer
– renanvm
I tried but without success. I also put return as Responseentity and a String as body and still kept returning null. Maybe the problem could be in the backend, but I can’t identify.
– Bruno Batista
Other non-delete methods are working?
– Erick Luz
Yes. Everything is normal. The backend works correctly, only the Angular that is not getting this answer "OK 204" even returning it.
– Bruno Batista