Aparece a mensagem no console do intellij, mas no insomnia continua com o "No body returned for response"
Segue o código das classes:
package med.voll.api.infra.exception;
import jakarta.persistence.EntityNotFoundException;
import med.voll.api.domain.ValidacaoException;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class TratadorDeErros {
@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity tratarErro404(){
return ResponseEntity.notFound().build();
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity tratarErro400(MethodArgumentNotValidException exception){
var erros = exception.getFieldErrors();
return ResponseEntity.badRequest().body(erros.stream().map(DadosErroValidacao::new).toList());
}
@ExceptionHandler(ValidacaoException.class)
public ResponseEntity tratarErroRegraDeNegocio(ValidacaoException ex) {
return ResponseEntity.badRequest().body(ex.getMessage());
}
private record DadosErroValidacao(String campo, String mensagem){
public DadosErroValidacao(FieldError erro){
this(erro.getField(), erro.getDefaultMessage());
}
}
}
package med.voll.api.controller;
import jakarta.validation.Valid;
import med.voll.api.domain.consulta.AgendaDeConsultas;
import med.voll.api.domain.consulta.DadosAgendamentoConsulta;
import med.voll.api.domain.consulta.DadosCancelamento;
import med.voll.api.domain.consulta.DadosDetalhamentoConsulta;
import org.apache.coyote.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("consultas")
public class ConsultaController {
@Autowired
private AgendaDeConsultas agenda;
@PostMapping
@Transactional
public ResponseEntity agendar(@RequestBody @Valid DadosAgendamentoConsulta dados) {
var dto = agenda.agendar(dados);
return ResponseEntity.ok(dto);
}
@DeleteMapping
@Transactional
public ResponseEntity cancelar(@RequestBody @Valid DadosCancelamento dados){
agenda.cancelar(dados);
return ResponseEntity.noContent().build();
}
}