Skip to content

Commit d56c912

Browse files
committed
https://blog.burakkutbay.com
1 parent 117efa8 commit d56c912

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

src/main/java/com/burakkutbay/springbootexceptionhandler/exception/CustomExceptionHandler.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
import org.springframework.http.HttpStatus;
44
import org.springframework.http.ResponseEntity;
5-
import org.springframework.web.bind.annotation.ControllerAdvice;
65
import org.springframework.web.bind.annotation.ExceptionHandler;
76
import org.springframework.web.bind.annotation.RestControllerAdvice;
8-
import org.springframework.web.servlet.NoHandlerFoundException;
97

10-
import javax.persistence.EntityNotFoundException;
8+
import java.util.ArrayList;
9+
import java.util.List;
1110

1211
/**
1312
* @author : Burak KUTBAY
@@ -17,13 +16,19 @@
1716
public class CustomExceptionHandler {
1817

1918
@ExceptionHandler(CustomerNotFoundException.class)
20-
public ResponseEntity<String> customerNotFoundException(CustomerNotFoundException customerNotFoundException) {
21-
return new ResponseEntity<String>(customerNotFoundException.getMessage(), HttpStatus.BAD_REQUEST);
19+
public ResponseEntity<?> customerNotFoundException(CustomerNotFoundException customerNotFoundException) {
20+
List<String> detail = new ArrayList<>();
21+
detail.add(customerNotFoundException.getMessage());
22+
ErrorResponse errorResponse = new ErrorResponse("Customer Not Found", detail);
23+
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
2224
}
2325

24-
@ExceptionHandler({NoHandlerFoundException.class, EntityNotFoundException.class,CustomerNotNullException.class})
25-
public ResponseEntity<String> customerNotNull(CustomerNotNullException customerNotNullException) {
26-
return new ResponseEntity<String>(customerNotNullException.getMessage(), HttpStatus.BAD_REQUEST);
26+
@ExceptionHandler(CustomerNotNullException.class)
27+
public ResponseEntity<?> customerNotNull(CustomerNotNullException customerNotNullException) {
28+
List<String> detail = new ArrayList<>();
29+
detail.add(customerNotNullException.getMessage());
30+
ErrorResponse errorResponse = new ErrorResponse("Customer Not Null", detail);
31+
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
2732
}
2833

2934

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.burakkutbay.springbootexceptionhandler.exception;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
import java.util.List;
8+
9+
/**
10+
* /**
11+
*
12+
* @author : Burak KUTBAY
13+
* Date : 30.11.2021
14+
*/
15+
@AllArgsConstructor
16+
@Getter
17+
@Setter
18+
public class ErrorResponse {
19+
private String message;
20+
private List<String> details;
21+
}

src/main/java/com/burakkutbay/springbootexceptionhandler/service/CustomerService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class CustomerService {
2222

2323
public Customer addCustomer(Customer customer) {
2424
if (customer.getName().isBlank() || customer.getName().isEmpty()) {
25-
throw new CustomerNotNullException("Customer Name Not Null");
25+
throw new CustomerNotNullException("Customer name must be not null");
2626
}
2727
return customerRepository.save(customer);
2828

@@ -34,11 +34,14 @@ public List<Customer> findAllCustomer() {
3434

3535
public Customer getCustomerById(Long customerId) {
3636
Optional<Customer> optionalCustomer = customerRepository.findById(customerId);
37-
Customer customer = optionalCustomer.orElseThrow(() -> new CustomerNotFoundException("Customer Not Found"));
37+
Customer customer = optionalCustomer.orElseThrow(() -> new CustomerNotFoundException(customerId + " "));
3838
return customer;
3939
}
4040

4141
public void deleteCustomerById(Long customerId) {
42+
Optional<Customer> optionalCustomer = customerRepository.findById(customerId);
43+
Customer customer = optionalCustomer.orElseThrow(() -> new CustomerNotFoundException(customerId.toString()));
44+
4245
customerRepository.deleteById(customerId);
4346
}
4447

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11

2+
server.port=9865

0 commit comments

Comments
 (0)