Return new ResponseEntity(exceptionResponse, HttpStatus.BAD_REQUEST) Return new ResponseEntity(exceptionResponse, ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request)ĮxceptionResponse exceptionResponse= new ExceptionResponse(new Date(), ex.getMessage(), ex.getBindingResult().toString()) ![]() Public final ResponseEntity handleUserNotFoundExceptions(UserNotFoundException ex, WebRequest request) Return new ResponseEntity(exceptionResponse, method of ResponseEntityExceptionHandler class returning exception structure and specific status Public final ResponseEntity handleAllExceptions(Exception ex, WebRequest request)ĮxceptionResponse exceptionResponse= new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false)) defining exception handling for all the class CustomizedResponseEntityExceptionHandler extends method of ResponseEntityExceptionHandler class This is the method which is fired when a bad request occurs. To resolve this problem we will add a method handleMethodArgumentNotValid() which is defined in the ResponseEntityExceptionHandler class. When we create a RESTful services we need to think about consumer that how does the consumer know what is wrong. But the name should have less than five characters. Step 5: Open the Rest client Postman and send a POST request with new user name Tony k. Return String.format("User ", id, name, dob) ![]() Public User(Integer id, String name, Date dob) Step 5: Add annotation just above the dob variable. Step 4: Add annotation just above the name variable. Let suppose that name should have at least five characters and date of birth should be in past not in present. Now we will add validations in User class on name and date of birth. Return ResponseEntity.created(location).build() ![]() URI location=omCurrentRequest().path("/").buildAndExpand(sevedUser.getId()).toUri() method that posts a new user detail and returns the status of the user ResponseEntity User user) Throw new UserNotFoundException("id: "+ id) Import class UserDaoService List retriveAllUsers()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |