Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views7 pages

Exception Handling

The document outlines the implementation of exception handling in a Java Spring application, specifically for product-related operations. It details the creation of custom exceptions, such as ProductNotFoundException and ProductBadRequestException, and how to handle them globally using a GlobalExceptionHandler. Additionally, it includes steps for testing the API responses using POSTMAN and refactoring validation logic into a separate validator class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Exception Handling

The document outlines the implementation of exception handling in a Java Spring application, specifically for product-related operations. It details the creation of custom exceptions, such as ProductNotFoundException and ProductBadRequestException, and how to handle them globally using a GlobalExceptionHandler. Additionally, it includes steps for testing the API responses using POSTMAN and refactoring validation logic into a separate validator class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Exception in Java

1.​ As you can see, when you test the GET by ID method, and the resource doesn't exist, it is
necessary to return and correct the response, for example, our use of exception handling.

2.​ Test the method in POSTMAN


3.​ The errors you can see on the Run console

4.​ At the moment it would be a good idea to show the message in the api response. for that you need
to add the error include message in application.yml, restart and test again the api.
5.​ Customizing the Exceptions
Create the ProductNotFound.java in the exception package

package com.example.product.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.NOT_FOUND)
public class ProductNotFoundException extends RuntimeException{
public ProductNotFoundException() {
super("Product Not Found");
}
}

6.​ Modify the GetProductService.java


package com.example.product.service;

import com.example.product.IProductRepository;
import com.example.product.Query;
import com.example.product.exception.ProductNotFoundException;
import com.example.product.model.Product;
import com.example.product.model.ProductDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class GetProductService implements Query<Integer, ProductDto> {

private final IProductRepository iProductRepository;

public GetProductService(IProductRepository iProductRepository) {


this.iProductRepository = iProductRepository;
}
@Override
public ResponseEntity<ProductDto> execute(Integer id) {
Optional<Product> product = this.iProductRepository.findById(id);
if (product.isPresent()) {
return ResponseEntity.status(HttpStatus.OK).body(
product.map(ProductDto::new).get());
}
throw new ProductNotFoundException();
}
}

7.​ Run the method

8.​ Create a ErrorMessages enum class and Refactor ProductNotFoundException.java

package com.example.product.exception;

public enum ErrorMessages {


PRODUCT_NOT_FOUND("Product Not Found");
private final String message;

ErrorMessages(String message) {
this.message = message;
}

public String toString() {


return this.message;
}
}

9.​ Create a ErrorResponse.java in model package


package com.example.product.model;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class ErrorResponse {
private final String message;
}

10.​Create a GlobalExceptionHandler.java

11.​Run and Test the application

12.​Create a ProductBadRequestException.java
package com.example.product.exception;

public class ProductBadRequestException extends RuntimeException{


public ProductBadRequestException(String message) {
super(message);
}
}

13.​Modify GlobalExceptionHandler.java
package com.example.product.exception;

import com.example.product.model.ErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(ProductNotFoundException.class)
@ResponseBody
@ResponseStatus(HttpStatus.NOT_FOUND)
public ErrorResponse
handleProductNotFoundException(ProductNotFoundException
productNotFoundException) {
return new ErrorResponse(productNotFoundException.getMessage());
}

@ExceptionHandler(ProductBadRequestException.class)
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleProductBadRequest(ProductBadRequestException
productBadRequestException) {
return new ErrorResponse(productBadRequestException.getMessage());
}
}

14.​Modify the CreateProductService


15.​Test the method create Product with Postman

15. Refactor validation into ProductValidator.java in the validator package. ( Challenge for you)

You might also like