Tweaking HATEOAS with BOOT
Just before exploring more of HATEOAS, lets add Spring BOOT to the application.
Now Spring Boot needs no introduction, beacause of its CoC (Convention of Confuguration) approach, be it Standalone or Web Based, Spring Boot is always "My Dear" as it frees us from all the headaches of dependencies, configurations, servers (in case of Web Application) etc.
For more about Spring Boot, its extensive documentation is a must read.
Here my intention is not to introduce Spring Boot directly but to integrate it with the existing WebApplicationInitializer, using ApplicationContextInitializer(for profiling and loading different properties based on the profile selected), using Spring-Boot-Actuator for different metrics and others.
Lets start:
In our previous post we have discussed on the WebApplicationInitializer for the Spring HATEOAS project i.e. SpringHateosApplicationConfig.
All these things we are going to integrate it into Spring Boot StartUp class:
The annotation @SpringBootApplication, but why????? Most of the time we have our main class annotated with, @Configuration @EnableAutoConfiguration @ComponentScan
@SpringBootApplication provide a convenient alternative to all of that.
The Spring Boot Application class extends SpringBootServletInitializer. This class is a handy replacement for WebApplicationInitializer. With this class in hand we can bootstrap a Web Application by binding the servlet and filter mappings (Like we do in WebApplicationInitializer or web.xml).
WebApplicationInitializer is required when we build a war file and deploy it.
So with the help of dispatcherRegistration and filterRegistrationBean Bean we are binding here DispatcherServlet and a CharacterEncoding Filter.
Within the main Method we have used an ApplicationContextInitializer
Spring boot actuator has got a lot of default endpoints, which gets configured automatically when the application boots with Spring Boot having Spring Boot Actuator as one of its dependencies.
For example, http://localhost:8080/SpringHATEOAS/hateos/metrics provides memory usages, Heap Memory, Threads available and other statistics. In the above URL SpringHATEOAS being my Application Context root and hateos being the Spring Dispatcher Servlet URL mapping and metrics is the Actuator endpoint.
Other configured endpoints being:
1) /info
2) /mappings
3) /dump
4) /beans
5) /trace
6) /health
7) /configprops etc.
With Spring Boot Actuator we can also configure custom Endpoints, depicting the stats of the project according to our need.
This is in short about how we have integrated Spring Boot and Actuator in our project.
Now, lets move into our HATEOAS world again.
Now Spring Boot needs no introduction, beacause of its CoC (Convention of Confuguration) approach, be it Standalone or Web Based, Spring Boot is always "My Dear" as it frees us from all the headaches of dependencies, configurations, servers (in case of Web Application) etc.
For more about Spring Boot, its extensive documentation is a must read.
Here my intention is not to introduce Spring Boot directly but to integrate it with the existing WebApplicationInitializer, using ApplicationContextInitializer(for profiling and loading different properties based on the profile selected), using Spring-Boot-Actuator for different metrics and others.
Lets start:
In our previous post we have discussed on the WebApplicationInitializer for the Spring HATEOAS project i.e. SpringHateosApplicationConfig.
All these things we are going to integrate it into Spring Boot StartUp class:
@Import(SpringHateosWebApplicationContext.class) @SpringBootApplication class HATEOASBoot extends SpringBootServletInitializer { public static void main(final String[] args) { //SpringApplication.run(HATEOASBoot.class, args); MapThe very first Line @Import(SpringHateosWebApplicationContext.class) , we are importing all the beans.mapProperties = new HashMap (); mapProperties.put("spring.profiles.active", "prod"); new SpringApplicationBuilder(HATEOASBoot.class).initializers(new SpringHateosApplicationContextInitializer())/*.properties(mapProperties)*/.run(args); } @Bean public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) { ServletRegistrationBean registration = new ServletRegistrationBean( dispatcherServlet); registration.addUrlMappings("/hateos/*"); return registration; } @Bean public FilterRegistrationBean filterRegistrationBean () { CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); FilterRegistrationBean registrationBean = new FilterRegistrationBean(); registrationBean.setFilter(characterEncodingFilter); Map initParameterMap = new HashMap (); initParameterMap.put("encoding", "UTF-8"); initParameterMap.put("forceEncoding", "true"); registrationBean.setInitParameters(initParameterMap); registrationBean.setAsyncSupported(true); registrationBean.addUrlPatterns("/*"); return registrationBean; } }
The annotation @SpringBootApplication, but why????? Most of the time we have our main class annotated with, @Configuration @EnableAutoConfiguration @ComponentScan
@SpringBootApplication provide a convenient alternative to all of that.
The Spring Boot Application class extends SpringBootServletInitializer. This class is a handy replacement for WebApplicationInitializer. With this class in hand we can bootstrap a Web Application by binding the servlet and filter mappings (Like we do in WebApplicationInitializer or web.xml).
WebApplicationInitializer is required when we build a war file and deploy it.
So with the help of dispatcherRegistration and filterRegistrationBean Bean we are binding here DispatcherServlet and a CharacterEncoding Filter.
Within the main Method we have used an ApplicationContextInitializer
public class SpringHateosApplicationContextInitializer implements ApplicationContextInitializerWithin ApplicationContextInitializer, we fetched the profile from within profile.properties and set it as an active profile within ConfigurableEnvironment. Along with this we have dynamically set the activeProfile value for the key "spring.profiles.active" with the help of MutablePropertySources by adding it as the Last Property with MapPropertySource, then we have used it within SpringHateosWebApplicationContext (discussed in the previous post), in the form of:{ public void initialize(ConfigurableApplicationContext configurableApplicationContext) { // TODO Auto-generated method stub ClassPathResource classPathResource = (ClassPathResource) configurableApplicationContext.getResource("classpath:profile.properties"); ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource(); resourceBundleMessageSource.setBasename("profile"); String activeProfile = resourceBundleMessageSource.getMessage("active.profile", null, Locale.US); ConfigurableEnvironment configurableEnvironment = configurableApplicationContext.getEnvironment(); configurableEnvironment.setActiveProfiles(activeProfile); MutablePropertySources mutablePropertySources = configurableEnvironment.getPropertySources(); Map propertyValuesMap = new HashMap (); propertyValuesMap.put("spring.profiles.active", activeProfile); mutablePropertySources.addLast(new MapPropertySource("APP_STARTUP_TIME_MUTABLE_HATEOAS_PROPS", propertyValuesMap)); } }
@PropertySources({@PropertySource("classpath:zookeeper.properties"),@PropertySource("classpath:hateoas-${spring.profiles.active}.properties")}) AND @Value("${spring.profiles.active}") private String activeProfile;So, with the help of PropertySources and spring.profiles.active and have dynamically added .properties files in the Application Context based on the active profile and then based on it we have initialized bean as:
@Profile("dev") @Qualifier("db") @Bean public String dbDev() { System.out.println("******** The PROPERTY DB IS FROM PROFILE DEV :"+db+" for activeProfile:"+activeProfile); return db; } @Profile("prod") @Qualifier("db") @Bean public String dbProd() { System.out.println("******* The PROPERTY DB IS FROM PROFILE PROD :"+db+" for activeProfile:"+activeProfile); return db; }To get various metrics about the project we have used Spring-Boot-Actuator..
Spring boot actuator has got a lot of default endpoints, which gets configured automatically when the application boots with Spring Boot having Spring Boot Actuator as one of its dependencies.
For example, http://localhost:8080/SpringHATEOAS/hateos/metrics provides memory usages, Heap Memory, Threads available and other statistics. In the above URL SpringHATEOAS being my Application Context root and hateos being the Spring Dispatcher Servlet URL mapping and metrics is the Actuator endpoint.
Other configured endpoints being:
1) /info
2) /mappings
3) /dump
4) /beans
5) /trace
6) /health
7) /configprops etc.
With Spring Boot Actuator we can also configure custom Endpoints, depicting the stats of the project according to our need.
This is in short about how we have integrated Spring Boot and Actuator in our project.
Now, lets move into our HATEOAS world again.
Comments
Post a Comment