Clique com o botão direito em
controllers
new, Java Class dê um nome de
MathController
Insira o código abaixo
| Arquivo: MathController.java |
| package br.com.iatagan.controllers; import br.com.iatagan.exception.UnsupportedMathOperationException; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/math") // Define o path base para o controlador public class MathController { // http://localhost:8080/math/sum/8/5 @RequestMapping(value="/sum/{numberOne}/{numberTwo}") public Double sum(@PathVariable("numberOne") String numberOne, @PathVariable("numberTwo") String numberTwo) throws Exception { if (!isNumeric(numberOne) || !isNumeric(numberTwo)) { throw new UnsupportedMathOperationException("Please set a numeric value!"); } return covertToDouble(numberOne) + covertToDouble(numberTwo); } public static Double covertToDouble(String strNumber) { if (strNumber == null) return 0d; String number = strNumber.replaceAll(",", ".");// Moeda Americana x Brasileira return Double.parseDouble(number); } public static boolean isNumeric(String strNumber) { if (strNumber == null) return false; String number = strNumber.replaceAll(",", "."); return number.matches("[-+]?[0-9]*\\.?[0-9]+"); } } |
Clique com o botão direito em
br.com.iatagan
new, package dê um nome de
exception
Crie uma nova Classe na pasta
exception
new, Java Class dê um nome de
ExceptionResponse
Essa classe é Record
Insira o código abaixo
| Arquivo: ExceptionResponse.java |
| package br.com.iatagan.exception; import java.util.Date; public record ExceptionResponse(Date timestamp, String message, String details) {} |
Crie uma nova Classe na pasta
exception
new, Java Class dê um nome de
UnsupportedMathOperationException
Insira o código abaixo
| Arquivo: UnsupportedMathOperationException.java |
| package br.com.iatagan.exception; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(HttpStatus.BAD_REQUEST) public class UnsupportedMathOperationException extends RuntimeException { public UnsupportedMathOperationException(String message) { super(message); } } |
Crie um novo pacote na pasta
exception
Com o nome de
handler
Clique com o botão direito na pasta
handler
new, Java Class dê um nome de
CustomEntityResponseHandler
Insira o código abaixo
| Arquivo: CustomEntityResponseHandler.java |
| package br.com.iatagan.exception.handler; import br.com.iatagan.exception.ExceptionResponse; import br.com.iatagan.exception.UnsupportedMathOperationException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; import java.util.Date; @ControllerAdvice @RestController public class CustomEntityResponseHandler extends ResponseEntityExceptionHandler { @ExceptionHandler(Exception.class) public final ResponseEntity<ExceptionResponse> handleAllExceptions(Exception ex, WebRequest request) { ExceptionResponse response = new ExceptionResponse( new Date(), ex.getMessage(), request.getDescription(false)); return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR); } @ExceptionHandler(UnsupportedMathOperationException.class) public final ResponseEntity<ExceptionResponse> handleBadRequestExceptions(Exception ex, WebRequest request) { ExceptionResponse response = new ExceptionResponse( new Date(), ex.getMessage(), request.getDescription(false)); return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); } } |
Entre no navegador e digite
http://localhost:8080/math/sum/8/5
Vamos simular um erro
http://localhost:8080/math/sum/8/aaaaa