Skip to content

Commit 4a32d81

Browse files
Feign Client Implementation Done
1 parent 57b1785 commit 4a32d81

22 files changed

+384
-24
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.javatodev.finance.exception;
2+
3+
public class EntityNotFoundException extends SimpleBankingGlobalException{
4+
public EntityNotFoundException() {
5+
super("Requested entity not present in the DB.", GlobalErrorCode.ERROR_ENTITY_NOT_FOUND);
6+
}
7+
8+
public EntityNotFoundException (String message) {
9+
super(message, GlobalErrorCode.ERROR_ENTITY_NOT_FOUND);
10+
}
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.javatodev.finance.exception;
2+
3+
public class ErrorResponse {
4+
private String code;
5+
private String message;
6+
7+
public String getCode() {
8+
return code;
9+
}
10+
11+
public void setCode(String code) {
12+
this.code = code;
13+
}
14+
15+
public String getMessage() {
16+
return message;
17+
}
18+
19+
public void setMessage(String message) {
20+
this.message = message;
21+
}
22+
23+
public ErrorResponse(String code, String message) {
24+
this.code = code;
25+
this.message = message;
26+
}
27+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.javatodev.finance.exception;
2+
3+
public class GlobalErrorCode {
4+
5+
public static final String ERROR_ENTITY_NOT_FOUND = "BANK-CORE-1000";
6+
public static final String INSUFFICIENT_FUNDS = "BANK-CORE-1001";
7+
8+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.javatodev.finance.exception;
2+
3+
import org.springframework.http.ResponseEntity;
4+
import org.springframework.web.bind.annotation.ControllerAdvice;
5+
import org.springframework.web.bind.annotation.ExceptionHandler;
6+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
7+
8+
import java.util.Locale;
9+
10+
@ControllerAdvice
11+
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
12+
13+
@ExceptionHandler(SimpleBankingGlobalException.class)
14+
protected ResponseEntity handleGlobalException(SimpleBankingGlobalException e, Locale locale) {
15+
return ResponseEntity
16+
.badRequest()
17+
.body(new ErrorResponse(e.getCode(), e.getMessage()));
18+
}
19+
20+
@ExceptionHandler({Exception.class})
21+
protected ResponseEntity handleException(Exception e, Locale locale) {
22+
return ResponseEntity
23+
.badRequest()
24+
.body("Exception occur inside API " + e);
25+
}
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.javatodev.finance.exception;
2+
3+
public class SimpleBankingGlobalException extends RuntimeException {
4+
5+
private String code;
6+
private String message;
7+
8+
public SimpleBankingGlobalException() {
9+
}
10+
11+
public SimpleBankingGlobalException(String code, String message) {
12+
this.code = code;
13+
this.message = message;
14+
}
15+
16+
public String getCode() {
17+
return code;
18+
}
19+
20+
public void setCode(String code) {
21+
this.code = code;
22+
}
23+
24+
public String getMessage() {
25+
return message;
26+
}
27+
28+
public void setMessage(String message) {
29+
this.message = message;
30+
}
31+
32+
}

core-banking-service/src/main/java/com/javatodev/finance/service/UserService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.javatodev.finance.service;
22

3+
import com.javatodev.finance.exception.EntityNotFoundException;
34
import com.javatodev.finance.model.dto.User;
45
import com.javatodev.finance.model.entity.UserEntity;
56
import com.javatodev.finance.model.mapper.UserMapper;
@@ -19,11 +20,11 @@ public class UserService {
1920
private final UserRepository userRepository;
2021

2122
public User readUser(String identification) {
22-
UserEntity userEntity = userRepository.findByIdentificationNumber(identification).get();
23+
UserEntity userEntity = userRepository.findByIdentificationNumber(identification).orElseThrow(EntityNotFoundException::new);
2324
return userMapper.convertToDto(userEntity);
2425
}
2526

2627
public List<User> readUsers(Pageable pageable) {
27-
return userMapper.convertToDtoList(userRepository.findAll(pageable).getContent());
28+
return userMapper.convertToDtoList(userRepository.findAll(pageable).getContent());
2829
}
2930
}

core-banking-service/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ spring:
22
application:
33
name: core-banking-service
44
datasource:
5-
url: jdbc:mysql://localhost:3306/banking_core_services
5+
url: jdbc:mysql://localhost:3306/banking_core_service
66
username: root
77
password: password
88
jpa:

internet-banking-user-service/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies {
2121
implementation 'org.springframework.boot:spring-boot-starter-actuator'
2222
implementation 'org.springframework.boot:spring-boot-starter-web'
2323
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
24+
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
2425
implementation ('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client') {
2526
exclude group: 'javax.ws.rs', module: 'jsr311-api'
2627
}

internet-banking-user-service/src/main/java/com/javatodev/finance/InternetBankingUserServiceApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
6+
import org.springframework.cloud.openfeign.EnableFeignClients;
67

8+
@EnableFeignClients
79
@EnableEurekaClient
810
@SpringBootApplication
911
public class InternetBankingUserServiceApplication {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.javatodev.finance.configuration;
2+
3+
import feign.codec.ErrorDecoder;
4+
import org.springframework.cloud.openfeign.FeignClientProperties;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
@Configuration
9+
public class CustomFeignClientConfiguration extends FeignClientProperties.FeignClientConfiguration {
10+
11+
@Bean
12+
public ErrorDecoder errorDecoder() {
13+
return new CustomFeignErrorDecoder();
14+
}
15+
}

0 commit comments

Comments
 (0)