Thursday, October 12, 2023

minimum web version required to use JSTL

 Q What is the minimal web version required to use JSTL?

And : 2.4

For example following tag from web.xml uses web 4.0

 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">  
 </web-app>  
d

Thursday, September 28, 2023

Create a bean of type java.util.properties

To create a bean of type java.util.properties in Java xml use the following code.


 <bean id="attendkey" class="java.util.Properties" name="attendenceKeys"/>  
 <bean id="attendkey.load" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">  
   <property name="targetObject"><ref bean="attendkey"/></property>  
   <property name="targetMethod"><value>putAll</value></property>  
   <property name="arguments">  
        <props>  
             <prop key="Present">1</prop>  
             <prop key="Absent" >-1</prop>  
       </props>  
  </property>  
 </bean>  

Example :

https://github.com/gauravmatta/springmvc/blob/master/book%20management%20system/src/main/java/com/springimplant/xml/config.xml

Enable Java EE Annotations, Java 9 onwards

  • Java EE or Java, enterprise Edition is not available by default since Java 9.
  • To access Java EE we need to include following library in Java 9
    • javax.annotation-api
  • Since @ Postconstruct and @Predestroy annotation Are parts of Java EE. We need to use following tag in our beans xml To enable all Java EE beans.
    • <context:annotation-config />
  • However, we may not require to enable all Java EE annotations in some cases Which is also a good programming practice.In such cases To enable only a single bean and it’s corresponding annotation we must declare that class as a bean Explicit in our xml file.
    • For example to Enable @postconstruct and @predestroy annotations, we must Declare following bean explicitly
      • <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

Saturday, August 19, 2023

Install Activiti Plugin Eclipse

Step 1: Open your IDE and click on Help » Install New Software…

Step 2: Click on the Add button and fill the details like Name: Activiti Designer (you can choose any name) Location: https://www.activiti.org/designer/update/ and then hit the OK button.

Finally proceed with next, next…, if it is able to load the contents.

If the above method did not work,

Step 1: Download zip from http://www.activiti.org/designer/archived/activiti-designer-5.18.0.zip

Step 2: Open your IDE and click on Help » Install New Software…

Step 3: Click on the Add button and fill the details like Name: Activiti Designer (you can choose any name) Location: [path/to/your/downloaded/zip/file] and then hit the OK button.


Sunday, April 30, 2023

AWS services in Spring Boot

  • Considering our Fargate Container has the hole to access the and execute AWS Services. We can use following code to access the Different Services. 
  • Remember The role of Fargate container is different from Build and Deployment creator role so the role of tools like Code Build,EC2 Image Builder may be different than role of Fargate container. The role of Fargate container or any other container in which our JVM is running and executing code matters here.
Q How can we call a Step Function using Service?
  •  private AWSStepFunctions awsStepfunctions;  
     awsStepfunctions = AWSStepFunctionsClientBuilder.standard().build();  
     //Step Function Parameters  
     JSONObject sfnInput = new JSONObject();  
     sfnInput.put("NAME", dataSetup.getname());  
     sfnInput.put("F_DATE", parsedfdate);  
     sfnInput.put("T_DATE", parsedtdate);  
          if (!StringUtils.hasLength(arn)) {  
                    arn = this.awsStateMachineArn;  
               }  
     try {  
                    StartExecutionRequest executionRequest = new StartExecutionRequest().withStateMachineArn(arn)  
                              .withInput(sfnInput.toString());  
                    StartExecutionResult result = this.awsStepfunctions.startExecution(executionRequest);  
                    log.info("Response from Execution " + arn + "=====>" + result.toString());  
               } catch (Exception e) {  
                    log.info(e.toString());  
               }  
sdf

Tuesday, May 5, 2020

Spring Rest Services and Microservices

Q Why should we handle response time out while calling Rest End point?
  • When we connect to server those connections are made with the help of threads. If a connection has not been established so thread will be blocked. So we will be having multiple blocked threads for each request since server is not responding. To avoid this situation and to release the threads we have to do a time out.
  • All these threads are available in thread pool in a server.
Q What is the difference between read time out and server time out?
  • Server timeout is when we are unable to establish a connection to the server.
  • Read time out happens when we are able to connect to server but unable to read the data that is we are not getting response back.
Q What is versioning in Rest?
  • When we update functionality in Rest and want user to use new one instead of old obsolete one.
  • We use versioning to mark these two different versions of functionality.
  • Versioning can be defined using.
    • Request parameters
      • @GetMapping(value="/courses", params="ver=2")
    • Headers
      • @GetMapping(value="/courses", headers="api-version=2")
    • Produces
      • @GetMapping(value="/courses", produces="application/version2+json")
    • URI
      • @GetMapping(value="/courses/v2")
Q How does basic authentication work in rest API?
  • We do it using SecurityConfiguration class in the API which extends to WebSecurityConfigurationAdapter.
  • When we hit the request it goes to AuthenticationFilter. The filter gives us an authentication object. This object is passed to AuthenticationManagerBuilder which will find the AuthenticationProvider. This provider will validate an authentication object.
  • There are various authentication providers like
    • DaoAuthenticationProvider
    • We can have our own custom authentication object provider.
  • This AuthenticationProvider will pass object to AuthenticationManagerBuilder. The builder will further pass the object to the SecurityContextHolder.
  • So SecurityContextHolder holds the SecurityContext object so when user signs in next time it won't ask for security credentials. It will check authentication credentials from the SecurityContextHolder itself. It will allow user to sign in without any credentials.
  • For authentication we can pass jwt authentication token in the headers which is used for authentication whenever we are giving call to the micro service.
    • This token is authenticated by the server.
    • This is called as token based authentication.
Q Which is better jwt based authentication or session based authentication?
  • Token based authentication should be followed in rest API's because those are stateless.
    • The server does not stores state of client side. Client sends all the information in the header so that server is able to process that request.
    • The client will give us a jwt token in every server we will have our public key or private key using which we will verify signature of the token and we will validate the token.
    • When we use basic token based authorization in the "Authorization Header" of the request we send "basic encoded_token_value".
      • Basic is the key word for basic authentication.
    • When we use jwt authentication we are writing bearar in place of basic which implies that we don't have any responsibility as a provider as we are using clients token. There is no further authentication needed once we have that token we are the owner of the token. The token has been generated after authentication with server.
      • jwt token is used for authorization of user and not authentication. Because the user who has jwt token is already authenticated.
  • Session based authentication is restricted to the particular session only. So if we hit a request do another system then that system won't recognize our session.
    • We may have many servers/instances running in microservices environment as we scale up our application.
    • Since session is limited to a server we can't use such an authentication in such a architecture.
Q What is content negotiation?
  • Content negotiation is what kind of requests should the API accept and what kind of response should API provide.
  • It is achieved by media type class in produces or consumes parameter of @Getmapping annotation as follows.
    • @GetMapping(value="/courses", consumes={MediaType.Application_XML_value}, produces={MediaType.Application_Json_Value})
Q What is the difference between @EnableDiscoveryClient and @EnableEurekaClient?

Ans
@EnableDiscoveryClient
  • This annotation is based onspring-cloud-commonsDependent and implemented in the classpath
  • If it is another registration center like (consul, zookeeper) then Eureka, @EnableDiscoveryClient is recommended

@EnableEurekaClient
  • The annotation is based onspring-cloud-netflixDependence can only be used for eureka;
  • if the registered center is eureka, then @EnableEurekaClient is recommended
Q Give some Examples of the different Rest Clients?
  • RestTemplate
  • OpenFin
Q How are rest templates used in a Project?
  • Rest Templates act as a client for consuming our Rest Data by hitting a url.
  • The url may send response in xml or json.
  • Example of Rest Template is
    • ProductDto productDto=restTemplate.getForObject(url,ProductDto.class,id);
  • Here ProductDto will be class with data members as same as what response is returned from url.
Q What is the difference between getForEntity() and getForObject()
  • Both methods belong to Rest Template
    • getForObject will only return an object i.e. response Object.
    • getForEntity will return a response Entity.
  • In getForEntity we also get values like statusCode(),getBody(),getHeaders().
  • We also have exchange() method to get Rest values.
Q What are the uses of Response Entity?
  • Using response Entity we can send the appropriate http status, response body, response headers.
  • For Example
    • return ResponseEntity.status(HTTPStatus.Accepted).body(restObject)
Q When an object is deleted and access is made what status code should we return?
  • Content not Found i.e. 204 we can return 200 too.


Friday, July 19, 2019

Spring IOC

What are the 2 types of Spring IOC containers?
  • Bean Factory
  • Application Context
What is the difference between bean factory and application context?
  • The spring framework comes with two IOC containers, bean factory and application context.
  • Bean factory is the most basic version of IOC container, and the application context extends the bean factory.
  • The application context comes with Advance features, including several that a gear towards enterprise applications while the bean factory comes with only basic features. Therefore, it is generally recommended to use application context and we should use bean factory only when memory consumption is critical.
  • Application context is an interface we can use its Implementation for creating objects in spring.
  • We can configure beans using XML,annotation and Java configuration file.

Spring Boot

What is circular/cyclic dependency in spring boot? When two services are interdependent on each other, that is to start one service, we requ...