SpringBoot with @FeignClient
1. create rest controller
@RestController
@RequestMapping("${ProfileBaseURL}")
@CrossOrigin
public class ProfileSearchController {
Logger logger = LogManager.getLogger(this.getClass());
@Autowired
CustomerSearchService customerSearchService;
/**
* Profile profile search based on id.
*
* @param customerSearchCriteria
* @return customerOverview
* @throws BusinessException
* @throws FileNotFoundException
* @throws CustomerServiceException
*/
@RequestMapping(method = RequestMethod.GET, value = "${SearchSingleURL}")
public ServiceResponse<Customer, ErrorResponse> getCustomerProfile(@PathVariable String customerId)
{
logger.debug("--> Customer profile search request:customerId {} ", customerId);
Customer customer = customerSearchService.getCustomerProfile(customerId);
ServiceResponse<Customer, ErrorResponse> response = new ServiceResponse<Customer,ErrorResponse>();
response.setHasErrors(HasErrors.FALSE);
response.setResponseObject(customer);
return response;
}
@RequestMapping("${healthcheck.url}")
public String checkHealth() {
return CustomerSearchConstants.ALIVE_STATUS_RESPONSE;
}
}
2. Create service interface
public interface CustomerSearchService {
Customer getCustomerProfile(String profileId) throws Exception;
}
2. Create serviceImpl
@Service
public class CustomerSearchServiceImpl implements CustomerSearchService {
private static final Logger LOGGER = LogManager.getLogger(CustomerSearchServiceImpl.class);
@Autowired
ProfileClient profileClient;
@Override
public Customer getCustomerProfile(String profileId) throws Exception {
return profileClient.getCustomerProfile(profileId);
}
@RestController
@RequestMapping("${ProfileBaseURL}")
@CrossOrigin
public class ProfileSearchController {
Logger logger = LogManager.getLogger(this.getClass());
@Autowired
CustomerSearchService customerSearchService;
/**
* Profile profile search based on id.
*
* @param customerSearchCriteria
* @return customerOverview
* @throws BusinessException
* @throws FileNotFoundException
* @throws CustomerServiceException
*/
@RequestMapping(method = RequestMethod.GET, value = "${SearchSingleURL}")
public ServiceResponse<Customer, ErrorResponse> getCustomerProfile(@PathVariable String customerId)
{
logger.debug("--> Customer profile search request:customerId {} ", customerId);
Customer customer = customerSearchService.getCustomerProfile(customerId);
ServiceResponse<Customer, ErrorResponse> response = new ServiceResponse<Customer,ErrorResponse>();
response.setHasErrors(HasErrors.FALSE);
response.setResponseObject(customer);
return response;
}
@RequestMapping("${healthcheck.url}")
public String checkHealth() {
return CustomerSearchConstants.ALIVE_STATUS_RESPONSE;
}
}
2. Create service interface
public interface CustomerSearchService {
Customer getCustomerProfile(String profileId) throws Exception;
}
2. Create serviceImpl
@Service
public class CustomerSearchServiceImpl implements CustomerSearchService {
private static final Logger LOGGER = LogManager.getLogger(CustomerSearchServiceImpl.class);
@Autowired
ProfileClient profileClient;
@Override
public Customer getCustomerProfile(String profileId) throws Exception {
return profileClient.getCustomerProfile(profileId);
}
}
4. create client interface to connect SOA service. Define fallback serviceimpl
@FeignClient(name = "ProfileClient", url = "${BaseURL}", fallback = ProfileClientFallback.class)
public interface LoyaltyProfileClient {
/**
* <pre>
* <b>Description : </b>
* Retrieve customer profile by Id.
*
* @param profileSearchId
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "${profileSearchUrl}")
Customer getCustomerProfile(@PathVariable("profileSearchId") String persistentCustomerId);
}
@FeignClient(name = "ProfileClient", url = "${BaseURL}", fallback = ProfileClientFallback.class)
public interface LoyaltyProfileClient {
/**
* <pre>
* <b>Description : </b>
* Retrieve customer profile by Id.
*
* @param profileSearchId
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "${profileSearchUrl}")
Customer getCustomerProfile(@PathVariable("profileSearchId") String persistentCustomerId);
}
5. Create fallback service impl
@Component
public class ProfileClientFallback implements ProfileClient{
Logger logger = LogManager.getLogger(ProfileClientFallback.class);
@Value("${mock.profile.search.json}")
public String jsonFromFile;
/**
* <pre>
* <b>Description : </b>
* Retrieves mock data from JSON.
*
* @param
* @return Customer
* </pre>
*/
public Customer getCustomer() {
logger.debug("Falling Back to mock service for customer profile search");
return loadFromMockedJSONData();
}
/**
* <pre>
* <b>Description : </b>
* Retrieves mock data from JSON.
*
* @param
* @return Customer
* </pre>
*/
@Override
public Customer getCustomerProfile(String persistentCustomerId) {
return getCustomer();
}
/**
* <pre>
* <b>Description : </b>
* Converts JSON data to Customer POJO.
*
* @param
* @return Customer
* </pre>
*/
public Customer loadFromMockedJSONData()
{
logger.debug("Before loading mock data");
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
Customer customer = null;
try {
customer = mapper.readValue(jsonFromFile, Customer.class);
} catch (Exception exp) {
logger.error(exp);
throw new APIException("JSONFIleIssue", "JSON FIle Issue", "API");
}
logger.debug("After loading mock data",customer);
return customer;
}
}
6. Create fallback configuration component. This is importent step,otherwise fallback wont work
@Configuration
public class FallbackServiceConfiguration {
@Bean
ProfileClientFallback profileClientFallback() {
return new ProfileClientFallback();
}
}
6. Create fallback configuration component. This is importent step,otherwise fallback wont work
@Configuration
public class FallbackServiceConfiguration {
@Bean
ProfileClientFallback profileClientFallback() {
return new ProfileClientFallback();
}
}
7. Create customer-profile.properties for configs
# SOA service Base URL
BaseURL=https://qa-server.com
# SOA service URL
profileSearchUrl=/profile/v1/profileLite
# Mock data when SOA service down
mock.profile.search.json={"customerId": "9217547943","Member": [{"MemberId": "123456","Code": "LZ","Code1": "","Codes": [""],"availableCnt": 0,"lifeTimeCnt": 0}]}
# Rest Controller base URL
ProfileBaseURL=/ProfileAPI/1.0.0
# Rest controller method URL
SearchSingleURL=/customers/searchSingle/{customerId}
Postman GET Request: http://localhost:8443/ProfileAPI/1.0.0/customers/searchSingle/123456
Accept: application/json
Comments
Post a Comment