Monday, November 21, 2022

AWS Solution Architecture (Assorted Articles)

Assorted Set of Articles for AWS Solution Architecture
API Gateway, Step Functions, Elastic Beanstalk, RDS*

AWS Solution Architecture (Step Functions, API Gateway, EC2, RDS*)


( Spring Boot, Core Java, Java EE and REST )
(PoC, Research, Exploration and Prototypes)
-
AWS Step Functions : Invoke API Gateway 
https://lnkd.in/gGsdzHgU
-
AWS Elastic Beanstalk : Deploy Spring Boot 
https://lnkd.in/gEfwXMxk
-
AWS Step Functions - Features & Usage
https://lnkd.in/gXVpiPev
-
AWS Step Functions - I/O Manipulation & Exec 
https://lnkd.in/g7KezVQ8
-
AWS Step Functions - Integration, Logs & Docs 
https://lnkd.in/gjE6tKzz
-
[Subscribe to My Blog] https://lnkd.in/gHyFjCZ
You May Sign-In Using Your : Personal [ GMail ]
-

Spring Boot - Annotation Reference - Part 01/b

In continuation of the Part 01/a of this article, Let's continue on our journey of Spring Boot Annotations. So, How much percentage (depth and extense) of Spring Boot Annotations do you think you really know? This includes annotations in all it's glory and the power that they bring via all of it's 'options'. I am sure whatever your answer be, I am sure you will appreciate this quote from Albert Einstein.

'The more I learn, the more I realize how much I don't know.'





[Spring- Stereotype] 
 
@Service 
Service Layer usually holds the core business logic of an application. In Spring, we denote the interface or class that holds the business logic with this annotation.
 package xyz.sumithpuri.spring.boot.annotation.service;  
   
 import java.util.HashSet;  
   
 import org.springframework.stereotype.Service;  
   
 import xyz.sumithpuri.spring.boot.annotation.model.Book;  
   
 /**  
  * @author Sumith Puri  
  *  
  */  
 @Service  
 public class BookServiceImpl implements BookService {  
   
      HashSet<Book> bookList = new HashSet<Book>();  
   
      @Override  
      public HashSet<Book> findAllBook() {  
           if (bookList.isEmpty())  
                return null;  
           else  
                return bookList;  
      }  
   
      @Override  
      public Book findBookByID(long id) {  
           Book book = bookList.stream().filter(b -> b.getId() == id).findAny().orElse(null);  
           return book;  
      }  
     ....  


@Controller 
@RestController
@Controller is a specialized component that is primarily used in the web layer. It is typically used in combination with annotated handler methods based on the RequestMapping annotation. @RestController is annotated with @Controller and is used for web layer request handling.  Types that carry the @RestController annotation are treated as controllers where @RequestMapping methods assume @ResponseBody semantics by default.
   
 import java.util.HashSet;  
   
 import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.web.bind.annotation.DeleteMapping;  
 import org.springframework.web.bind.annotation.GetMapping;  
 import org.springframework.web.bind.annotation.PathVariable;  
 import org.springframework.web.bind.annotation.PostMapping;  
 import org.springframework.web.bind.annotation.RequestBody;  
 import org.springframework.web.bind.annotation.RestController;  
   
 import xyz.sumithpuri.spring.boot.annotation.configuration.SBASampleConfigurationProperties;  
 import xyz.sumithpuri.spring.boot.annotation.model.Book;  
 import xyz.sumithpuri.spring.boot.annotation.service.BookServiceImpl;  
   
 /**  
  * @author Sumith Puri  
  *  
  */  
 @RestController  
 public class SBASampleController {  
   
      @Autowired  
      BookServiceImpl bookServiceImpl;  
   
      @Autowired  
      SBASampleConfigurationProperties sbasConfigProps;  
        
   
      @GetMapping("/findall")  
      public HashSet<Book> getAllBook() {  
           return bookServiceImpl.findAllBook();  
      }  
   
      @GetMapping("/findbyid/{id}")  
      public Book geBookById(@PathVariable long id) {  
           return bookServiceImpl.findBookByID(id);  
      }  
     ...  

@Component  
@Component is used to create any Spring managed component. It can be used as a Spring Bean. Any bean with @Bean that is created within a component will have a 'Prototype' scope, as opposed to a 'Singleton' scope of beans that is created within a @Configuration annotated class. @Repository and @Controller are all specialized components
 package xyz.sumithpuri.spring.boot.annotation.component;  
   
 import javax.annotation.PostConstruct;  
   
 import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.stereotype.Component;  
   
 import xyz.sumithpuri.spring.boot.annotation.service.SBASampleInterface;  
   
 /**  
  * @author sumith.puri  
  *  
  */  
 @Component  
 public class SpringBootAnnotationComponent {  
   
        
      @Autowired  
      private SBASampleInterface sbaSampleInterfaceImpl;  
        
      @PostConstruct  
      private void postConstruct() {  
           System.out.println("Testing @SpringBootApplication, @Component and @PostConstruct");  
      }  
   
 }  


@Repository 
@Repository is a specialized @Component that is used to mark a class that provides persistence or storage operations. It will provide operations like create, update, retrieve, delete and search type of operations. It is mostly used in conjunction with RDBMS or any other Database 

  
[Spring - REST/Web/MVC]

@RequestMapping
This annotation is from MVC/Web that will associate a given URI with a method in the controller. It can be used in the following format.
 @RequestMapping(method = RequestMethod.PATCH)  

@GetMapping
This annotation is used to map a HTTP GET request to a specific handler method in the controller. It is equivalent to the following alternative.
 @RequestMapping(method = RequestMethod.GET) 

@PostMapping
This annotation is used to map a HTTP POST  request to a specific handler method in the controller. It is equivalent to the following alternative.
 @RequestMapping(method = RequestMethod.POST) 

@DeleteMapping
This annotation is used to map a HTTP DELETE request to a specific handler method in the controller. It is equivalent to the following alternative.
 @RequestMapping(method = RequestMethod.DELETE) 

@PutMapping
This annotation is used to map a HTTP PUT request to a specific handler method in the controller. It is equivalent to the following alternative.
 @RequestMapping(method = RequestMethod.PUT) 

@PatchMapping
This annotation is used to map a HTTP PATCH request to a specific handler method in the controller. It is equivalent to the following alternative.
 @RequestMapping(method = RequestMethod.PATCH) 

@RequestBody
This annotation is used to bind a method parameter/object to incoming request parameters. 

@ResponseBody
This is used inside a controller and signifies that the returned object will be automatically serialized and passed back into the HttpResponse object. Note that if you are using @RestController you may not need to use this as automatically it is a combination of @Controller and @ResponseBody.

@RequestParam
This is used to bind a method parameter directly to a request attribute.  

@RequestHeader
This is used to bind a method parameter directly to a request header.

@RequestAttribute 
This can be used to bind a method parameter to a request attribute that was added from an intermediary layer like filter or interceptor.

@PathVariable

This is used to bind a method parameter from a request template URI. Note that It can be used to bind multiple method parameters.

 package xyz.sumithpuri.spring.boot.annotation.controller;  
   
 import java.util.HashSet;  
   
 import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.web.bind.annotation.DeleteMapping;  
 import org.springframework.web.bind.annotation.GetMapping;  
 import org.springframework.web.bind.annotation.PathVariable;  
 import org.springframework.web.bind.annotation.PostMapping;  
 import org.springframework.web.bind.annotation.RequestBody;  
 import org.springframework.web.bind.annotation.RestController;  
   
 import xyz.sumithpuri.spring.boot.annotation.configuration.SBASampleConfigurationProperties;  
 import xyz.sumithpuri.spring.boot.annotation.model.Book;  
 import xyz.sumithpuri.spring.boot.annotation.service.BookServiceImpl;  
   
 /**  
  * @author Sumith Puri  
  *  
  */  
 @RestController  
 public class SBASampleController {  
   
      @Autowired  
      BookServiceImpl bookServiceImpl;  
   
      @Autowired  
      SBASampleConfigurationProperties sbasConfigProps;  
   
      @GetMapping("/findall")  
      public HashSet<Book> getAllBook() {  
           return bookServiceImpl.findAllBook();  
      }  
   
      @GetMapping("/findbyid/{id}")  
      public Book geBookById(@PathVariable long id) {  
           return bookServiceImpl.findBookByID(id);  
      }  
   
      @DeleteMapping("/delete")  
      public void deleteBook() {  
           bookServiceImpl.deleteAllData();  
      }  
   
      @PostMapping("/")  
      public void addBook(@RequestBody Book book) {  
   
           System.out.println("Testing Properties: " + sbasConfigProps.getName() + "; "   
                                                                  + sbasConfigProps.getMail() + "; "  
                                                                  + sbasConfigProps.getYear());  
           bookServiceImpl.addBook(book);  
      }  
 }  
   

(@RestController, @GetMapping, @PostMapping,
@DeleteMapping, @Autowired, @Pathvariable )



[Reference Links]
https://github.com/Buzzardo/spring-docs/blob/master/annotation-cheat-sheet.adoc
https://javasterling.com/java/spring-boot-annotations/


Tuesday, November 15, 2022

Spring Boot - Annotation Reference - Part 01/a

In 2018, I was introduced to the topic of Microservices and Spring Boot via a formal training. This was during my brief stint as a Senior Architect in Manila, Philippines. Though I had worked on a 'similar architecture' way back in 2007-'08 while working as a Software Engineer at Symantec - I found the idea of the Uber JAR really exciting. Also, since it will now be enforced via the most popular framework brings in more possibilities. This includes building 'executable applications' for windows much easier!  



Anyways, The topic of our discussion is Spring Boot Annotations. Recently, In August 2022 while training a team of 10 - I realized that though even though I know most of the Spring Boot Annotations, I may not be aware of all of them. So, I decided to write this article. I hope it helps the readers to have a quick glance either during their daily work or as a general reference. Since I have been working on Spring Boot, Cloud, Spring Security, Spring Data since the last 4 years, I will later write a [Part-02] of this article covering the other annotations as well. It will cover annotations of Spring Security, Spring Data and Spring Cloud.

Herein, I will try to cover the annotations that you may most frequently see in daily development. Some of them you may know vaguely or just seen them in code but not understood completely. This article help you refresh what you already know and also to know more about the ones you had just come across.

GitHub Repository

[Spring Boot]
 
@SpringBootApplication
Well, this might be surprising. @SpringBootApplication is actually a combination of three features or annotations. In other words, it has the effect of 3 annotations together : @EnableAutoConfiguration, @ComponentScan, @Configuration. 
 
The main class of your Spring Boot Application should be annotated with this annotation, which has a main method.
 package xyz.sumithpuri.spring.boot.annotation;  
   
 import org.springframework.boot.SpringApplication;  
 import org.springframework.boot.autoconfigure.SpringBootApplication;  
 import org.springframework.context.annotation.Bean;  
   
 import xyz.sumithpuri.spring.boot.annotation.service.SBASampleImpl;  
 import xyz.sumithpuri.spring.boot.annotation.service.SBASampleInterface;  
   
   
 @SpringBootApplication  
 public class SpringBootAnnotationApplication {  
   
      public static void main(String[] args) {  
           SpringApplication.run(SpringBootAnnotationApplication.class, args);  
      }  
        
      @Bean  
      public SBASampleInterface getSBAService() {  
             
           return new SBASampleImpl();  
      }  
 }  
   

@EnableAutoConfiguration 
So, Spring allows the automatic configuration of the application, by creating and registering the spring beans in the classpath. The @EnableAutoConfiguration allows to define the base search package. By default, the base package for searching of beans will be the same package as of the class that declares this annotation.

Usually, you will place this annotation on your main class. If you use @SpringBootApplication, you may not need this annotation.
 package xyz.sumithpuri.spring.boot.annotation;  
   
 import org.springframework.boot.SpringApplication;  
 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  
 import org.springframework.context.annotation.Bean;  
 import org.springframework.context.annotation.ComponentScan;  
 import org.springframework.context.annotation.Configuration;  
   
 import xyz.sumithpuri.spring.boot.annotation.service.SBASampleImpl;  
 import xyz.sumithpuri.spring.boot.annotation.service.SBASampleInterface;  
   
   
 //@SpringBootApplication  
 @EnableAutoConfiguration  
 @Configuration  
 @ComponentScan(basePackages = "xyz.sumithpuri.spring.boot.annotation")  
 public class SpringBootAnnotationApplication {  
   
      public static void main(String[] args) {  
           SpringApplication.run(SpringBootAnnotationApplication.class, args);  
      }  
        
      @Bean  
      public SBASampleInterface getSBAService() {  
             
           return new SBASampleImpl();  
      }  
 }  
   

@SpringBootTest
This one is straightforward, @SpringBootTest is used to create an application context object that supports testing.
 
You must annotate your Test Class file with this annotation
 package xyz.sumithpuri.spring.boot.annotation;  
   
 import org.junit.jupiter.api.Test;  
 import org.junit.runner.RunWith;  
 import org.springframework.boot.test.context.SpringBootTest;  
 import org.springframework.test.context.junit4.SpringRunner;  
   
 @RunWith(SpringRunner.class)  
 @SpringBootTest  
 class SpringBootAnnotationApplicationTests {  
   
      @Test  
      void contextLoads() {  
      }  
 }  
   

@SpringBootConfiguration
Even though I have not used it much in my applications, from what I could gather I have found it is already part of the @SpringBootApplication. The only difference that exists between @Configuration and @SpringBootConfiguration is that latter allows to automatically locate the configuration. This will be useful for unit and integration tests.


@ConditionalOnClass
Will match only when the specified classes are in the classpath.
 package xyz.sumithpuri.spring.boot.annotation.configuration;  
   
 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;  
 import org.springframework.context.annotation.Bean;  
 import org.springframework.context.annotation.Configuration;  
   
 import xyz.sumithpuri.spring.boot.annotation.service.SBASampleImpl;  
 import xyz.sumithpuri.spring.boot.annotation.service.SBASampleInterface;  
   
 /**  
  * @author sumith.puri  
  *  
  */  
 @Configuration  
 @ConditionalOnClass(SBASampleImpl.class)  
 public class SBASampleConfiguration {  
        
      @Bean  
      public SBASampleInterface getSBAService() {  
             
           return new SBASampleImpl();  
      }  
 }  
   

With Spring DevTools Enabled, you will see one such log on the console that are the debug statements showing the matches or evaluations against the conditions.


@ConditionalOnProperty
Will match only when the specified environment property is present and it has a specific value.
 package xyz.sumithpuri.spring.boot.annotation.configuration;  
   
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;  
 import org.springframework.context.annotation.Bean;  
 import org.springframework.context.annotation.Configuration;  
   
 import xyz.sumithpuri.spring.boot.annotation.service.SBASampleImpl;  
 import xyz.sumithpuri.spring.boot.annotation.service.SBASampleInterface;  
   
 /**  
  * @author sumith.puri  
  *  
  */  
 @Configuration  
 //@ConditionalOnClass(SBASampleImpl.class)  
 @ConditionalOnProperty(name="mode", havingValue="false")  
 public class SBASampleConfiguration {  
        
      @Bean  
      public SBASampleInterface getSBAService() {  
             
           return new SBASampleImpl();  
      }  
 }  
 
Please go ahead and add the property 'mode=false' in your application.properties If this property is not present or has a different value, your server will refuse to start as there will be not property present to inject for an autowired bean. (Refer to the code in the GitHub Repository).





With Spring DevTools Enabled, you will see one such log on the console that are the debug statements showing the matches or evaluations against the conditions.


@ConfigurationProperties
@ConfigurationPropertiesScan
It marks a class as a configuration properties source (mapping it from a properties or yaml file), which can then be used to control and also to validate properties. ConfigurationPropertiesScan can be used to scan locations for property files. The location can be specified as the parameter to the annotation.

 package xyz.sumithpuri.spring.boot.annotation.configuration;  
   
 import org.springframework.boot.context.properties.ConfigurationProperties;  
 import org.springframework.boot.context.properties.ConfigurationPropertiesScan;  
 import org.springframework.stereotype.Component;  
   
 /**  
  * @author sumith.puri  
  *  
  */  
 @Component  
 @ConfigurationProperties(prefix = "proptest")  
 @ConfigurationPropertiesScan  
 public class SBASampleConfigurationProperties {  
   
      private String name;  
      private String pass;  
      private String mail;  
      private String year;  
      private long uuid;  
   
      public String getName() {  
           return name;  
      }  
   
      public void setName(String name) {  
           this.name = name;  
      }  
   
     ..... // Getter and Setter Methods 

[Typical Properties File to be Read By ConfigurationProperties]










[Debug Print Messages on Invocation of a Controller Endpoint]














[Reference Links]
https://github.com/Buzzardo/spring-docs/blob/master/annotation-cheat-sheet.adoc
https://javasterling.com/java/spring-boot-annotations/