Showing posts with label SpringBoot. Show all posts
Showing posts with label SpringBoot. Show all posts

Friday, September 13, 2019

REST API Documentation using Swagger2 in Spring Boot

Spring Boot makes developing RESTful services ridiculously easy .  Using Swagger  we can makes documenting our RESTful services easily.

Building a back-end API layer introduces a whole new area of challenges that goes beyond implementing just endpoints. Clients which uses our API,need to know how to interact with our API. In SOAP-based web services, you had a WSDL to work with. This gave API developers an XML-based contract, which defined the API. However, with RESTFul web services, there is no WSDL. So Rest  API documentation becomes more critical due to those reasons .











                                                                                                                      Version -  1.0.0

Tuesday, January 22, 2019

Singleton Class in Java

In object-oriented programming, a singleton class is a class that can have only one object (an instance of the class) at a time.
After first time, if we try to instantiate the Singleton class, the new variable also points to the first instance created. So whatever modifications we do to any variable inside the class through any instance, it affects the variable of the single instance created and is visible if we access that variable through any variable of that class type defined.
To design a singleton class:
  1. Make constructor as private.
  2. Write a static method that has return type object of this singleton class. Here, the concept of Lazy initialization in used to write this static method
public class Singleton {
    private static final Singleton instance = new Singleton();

    private Singleton() {}

    public static Singleton getInstance() {
        return instance;
    }
}

In terms of practical use Singleton patterns are used in logging, caches, thread pools, configuration settings, device driver objects.
 
Configuration File: This is another  usage of Singleton pattern because this has a performance benefit as it prevents multiple users to repeatedly access and read the configuration file or properties file. It creates a single instance of the configuration file which can be accessed by multiple calls concurrently as it will provide static config data loaded into in-memory objects. The application only reads from the configuration file at the first time and there after from second call onwards the client applications read the data from in-memory objects

We can use the cache as a singleton object as it can have a global point of reference and for all future calls to the cache object the client application will use the in-memory object

Why can’t we use a static class instead of singleton?

  • One of the key advantages of singleton over static class is that it can implement interfaces and extend classes while the static class cannot (it can extend classes, but it does not inherit their instance members). If we consider a static class it can only be a nested static class as top level class cannot be a static class. Static means that it belongs to a class it is in and not to any instance. So it cannot be a top level class.
  • Another difference is that static class will have all its member as static only unlike Singleton.
  • Another advantage of Singleton is that it can be lazily loaded whereas static will be initialized whenever it is first loaded.
  • Singleton object stores in Heap but, static object stores in stack.
  • We can clone the object of Singleton but, we can not clone the static class object.
  • Singleton can use the Object Oriented feature of polymorphism but static class cannot.

References Used :- singleton-class by geeksforgeeks 
                               dzone.com- singleton-design-pattern

 Version :- 1.1.0

Thursday, December 27, 2018

How to Change PrimeFace Theme According to User Preferences in SpringBoot Project


In web.xml  file  add  this   ,

    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>#{settingsController.userTheme}</param-value>
    </context-param>


Our  controller  class  will be like this ,

@ManagedBean(name = "settingsController")
@SessionScoped
@Controller
public class SettingsController {
    
    private String userTheme = "glass-x" ;  //Initial Theme 
    private Map<String , String>  themeMap ;
    
    @PostConstruct
    public void init (){
       setThemeMapInit(  );
    }
    
    public String getUserTheme() {
        return userTheme;
    }

    public void setUserTheme(String userTheme) {

        this.userTheme = userTheme;
    }

    public Map<String, String> getThemeMap() {

        return themeMap;
    }
  
    public void setThemeMapInit() {
        themeMap =  new LinkedHashMap<String, String>();
        themeMap.put("Aristo", "aristo");
        themeMap.put("After-noon", "afternoon");
        themeMap.put("After-Work", "afterwork");
        themeMap.put("Black-Tie", "black-tie");
        themeMap.put("Blitzer", "blitzer");
        themeMap.put("Bluesky", "bluesky");
        themeMap.put("Bootstrap", "bootstrap");
        themeMap.put("Casablanca", "casablanca");
        themeMap.put("Cupertino", "cupertino");
        themeMap.put("Dark-Hive", "dark-hive");
        themeMap.put("Delta", "delta");
        themeMap.put("Excite-Bike", "excite-bike");
        themeMap.put("Flick", "flick");
        themeMap.put("Glass-X", "glass-x");
        themeMap.put("Home", "home");
        themeMap.put("Hot-Sneaks", "hot-sneaks");
        themeMap.put("Humanity", "humanity");
        themeMap.put("Overcast", "overcast");
        themeMap.put("Pepper-Grinder", "pepper-grinder");
        themeMap.put("Redmond", "redmond");
        themeMap.put("Rocket", "rocket");
        themeMap.put("Sam", "sam");
        themeMap.put("Smoothness", "smoothness");
        themeMap.put("South-Street", "south-street");
        themeMap.put("Start", "start");
        themeMap.put("Sunny", "sunny");
        themeMap.put("Swanky-Purse", "swanky-purse");
        themeMap.put("UI-Lightness", "ui-lightness");

    }

    
    public void setThemeMap(Map<String, String> themeMap) {  
     this.themeMap =themeMap;
    }
    
    public void  sumbitUserSettings (){
        System.out.println("******  User  Theme ****** " +  userTheme );
    }

}

 


Finally  our xhtml  file  will  be like  this

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core">

    <body>

        <ui:composition template="./mainTemplate.xhtml">

            <ui:define name="content">


                <h1 class="ui-widget ui-widget-header" style=" font-size: 15px;

                    font: bold;line-height: 2.8em;text-align: center;">Settings</h1>


                <h:form id="changeThemeFormId" >


                    <p:growl id="
changeThemeFormMsg" showDetail="true"   showSummary="false" />
                   
                        <h:panelGrid columns="3" cellpadding="5" styleClass="add-form">   
                            
                            <h:outputLabel for="userTheme" value="Theme Name  *:" style="width: 300px"/> 
                            <p:selectOneMenu id="userTheme" value="#{settingsController.userTheme}"  style="width:200px" 
                                             required="true" requiredMessage="Theme Name is Required" > 
                                <f:selectItems value="#{settingsController.themeMap}"/> 
                            </p:selectOneMenu>
                            <p:message for="userTheme" display="icon"/>

                            <p:commandButton value="Sumbmit" styleClass="ui-priority-primary" style="font-weight:bold; font-size:13px" 

                                            resetValues="ttrue"  actionListener="#{settingsController.sumbitUserSettings()}"/>

                        </h:panelGrid> 


                    </p:fieldset>


                </h:form>


            </ui:define>


        </ui:composition>

    </body>
</html>


Version  1.1.0

Tuesday, December 18, 2018

Dependencies for Spring Boot Project

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>

</dependencies>
  • spring-boot-starter         – this lets spring boot autoconfigure your application
  • spring-boot-starter-web – this dependency tells spring boot that your application is a web application. This add the functionality of @Controller, @RequestMapping, etc.
  • spring-boot-starter-thymeleaf – web app needs some views like jsp or html. Thymeleaf is a server side template engine where your htmls can be viewed in any browser. Simply to say, instead of traditional jsp with jstl or format tags.
  • spring-boot-devtools     – this is an optional dependency but this makes your development faster. This dependency automatically restart or hotswap your changes to the running tomcat server, thus removing the need to restart the application whenever there are changes in codes.

Some interesting things to explore more

 Here  some  some  things  to  study  more ,     How Google Search works               https://developers.google.com/search/docs/fundamental...