Posts

Showing posts from June, 2019

Fien client

@FeignClient(name = "testClient", url = "${actual.url}" , fallback = ClientFallback.class) public interface testClient { /** * * @return res * * */ @RequestMapping(method = RequestMethod.GET, value ="/content/dl/messages.msgpage.json", consumes = "application/json") String getsByAppName(); /** * * @return res * * */ @RequestMapping(method = RequestMethod.GET, value ="/content/dl/exception.messages.axismsgpage.json", consumes = "application/json") String getAemExceptionMessages(); } 2. Client Fallback impl public class ClientFallback implements testClient { Logger logger = LogManager.getLogger(ClientFallback.class); @Value("${fallback.message}") private String fallbackJson; @Value("${exception.message}") private String exceptionFallbackJson; @Override public String getExceptionMessages() { return getFallbackExceptionJson(); } @Override...

SpringBoot controller advice

@ControllerAdvice @Order(Ordered.HIGHEST_PRECEDENCE) public class TestExceptionHandler { Logger logger = LogManager.getLogger(TestExceptionHandler.class); @Value(value = "${errorexceptionhandling.validation.name}") String validationComponentName; @Autowired ErrorExceptionMessageHandler errorHandler; @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<BaseServiceResponse<ErrorResponse>> validationErrorHandler( MethodArgumentNotValidException ex) { List<FieldError> list = ex.getBindingResult().getFieldErrors(); List<ErrorResponse> errorList = new ArrayList<>(); for (FieldError fe : list) { Map<String, String> params = new HashMap<>(); params.put(ExceptionHandlerConstants.FIELD, fe.getField()); Object temp = fe.getRejectedValue(); if (temp instanceof String) { params.put(ExceptionHandlerConstants.VALIDATED_VALUE, (String) temp); } if (fe.contains(Con...

SpringBoot default application

@SpringBootApplication @PropertySource("/error-exception-handling-application.properties") @EnableCircuitBreaker @EnableAspectJAutoProxy @EnableFeignClients @ComponentScan({ "com.test" }) @EnableAutoConfiguration(exclude = { SpringApplicationAdminJmxAutoConfiguration.class, DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class, JmxAutoConfiguration.class }) public class ErrorHandlingApplication { public static void main(String[] args) { SpringApplication.run(ErrorHandlingApplication.class, args); } }

Unit test with mock MVC

Junit test with Mock MVC @RunWith(SpringJUnit4ClassRunner.class) @EnableCircuitBreaker @EnableAspectJAutoProxy @ContextConfiguration(classes = {ProfileSearchController.class}) @WebMvcTest(ProfileSearchControllerTest.class) @TestPropertySource({ "classpath:customerprofileservice.properties" }) //customerprofileservice.properties has configs required for running tests public class ProfileSearchControllerTest { @Autowired ProfileSearchController controller; @Autowired private MockMvc mockMvc; //This is required as Controller to invoking service component @MockBean ProfileSearchService searchService; ///ProfileAPI/1.0.0/customers/searchSingle/123456  Post man URL without baseUrl @Test public void testGetCustomerProfile() throws Exception { mockMvc.perform(get("/ProfileAPI/1.0.0/customers/searchSingle/123456")) .andExpect(status().isOk()); } } 2. create customerprofileservice.properties. In this example,this property file i...

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>(); res...