Spring Boot real-time Interview questions




Q.How to encrypt passwords in a Spring Boot project in Jasypt.

Ans-Spring boot is a Java-based framework to develop microservices in order to build enterprise-level applications.

You often come across developing projects where you have to connect to databases like MongoDB, etc and store the authentic password of DB connection in the config file of spring boot project (application.yml or application.properties). Even passwords or tokens required for Authorization to make other API call are also stored in the same way.

You can actually refrain from adding the actual password in the config file and use ‘jasypt-spring-boot‘, a java library.

What is Jasypt?

Jasypt (Java Simplified Encryption), provides encryption support for property sources in Spring Boot Applications. It will help you to add basic encryption features to your projects with very fewer efforts and without writing any code with the help of a few additions in your project here and there. Springboot is a very powerful framework that will help you to add encryption capability without implementing any cryptography method. Jasypt is highly configurable.

 

1.    Add maven dependency of jasypt: In the pom.xml file, add maven dependency which can be found easily at maven repository.

2.    Add annotation in the Spring Boot Application main Configuration class: @EnableEncryptableProperties annotation needs to be added to make the application understand the encryptable properties across the entire Spring Environment.

3.    Decide a secret key to be used for encryption and decryption The secret key is used to encrypt the password and later can be used to decrypt the encrypted value to get the actual password. You can choose any value as the secret key.

4.    Generate Encrypted Key The encrypted key can be generated through either of the following 2 methods:

a.    Use the Jasypt Online Tool :

This link can be used to generate an encrypted key by passing the chosen secret key.

·         The password to encrypt: abcd1234

·         Select type of encryption: Two-way encryption (PBEWithMD5AndDES by default is used)

·         Secret Key: hello (It can be any value)

·         Encrypted String: kNuS1WAezYE7cph7zXVTiPSQSdHTx7Kv

You can actually use the tool to encrypt and check the encrypted key by decrypting it.

b.    Use the jasypt Jar: Download the jasypt jar file from the maven repository and run it through the following command:

java -cp //jasypt-1.9.3/lib/jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input=”xyz123″ password=secretkey algorithm=PBEWithMD5AndDES

Following is the significance of command-line parameters passed to run the jar:

·         input: abcd1234 (Actual password to be encrypted)

·         password: hello (the secret key chosen by you)

·         algorithm: PBEWithMD5AndDES (default algorithm used)

·         OUTPUT: scEjemHosjc/hjA8saT7Y6uC65bs0swg (Encrypted value of input)

Note: Though the encrypted value i.e. Encrypted String & OUTPUT in 3.1 and 3.2 respectively are different, as the secret key is the same, the decryption will result in the same value (abcd1234) in both the cases.

5.    Add the encrypted key in the config file (application.yml or application.properties): Now instead of adding the actual password ie. “abcd1234” as per the above eg., you need to add the encrypted value generated by either of the above methods. But how will the jasypt dependency understand that the particular property of the config file needs to be decrypted? Hence to make Jasypt aware of your encrypted values, it uses a convention which you need to add in the following format:

ENC(encrypted key): ENC(scEjemHosjc/hjA8saT7Y6uC65bs0swg)

In the above image, the encryption of the database password is done. You can use it in any scenario where you have to hide the actual password.

6.    Secret key chosen needs to be passed to decrypt at runtime: Make the Jasypt aware of the secret key which you have used to form the encrypted value. Hence following are the different methods to pass the secret key: 

a.    Pass it as a property in the config file. Run the project as usual and the decryption would happen.

b.    Run the project with the following command:

$mvn-Djasypt.encryptor.password=secretkey spring-boot:run

c.    Export Jasypt Encryptor Password:

JASYPT_ENCRYPTOR_PASSWORD=hello

2. How to publish JSON messages on Apache Kafka in Spring boot.

Ans-Apache Kafka is a publish-subscribe messaging system. A messaging queue lets you send messages between processes, applications, and servers. In this article, we will see how to send JSON messages to Apache Kafka in a spring boot application.

In order to learn how to create a spring boot project, refer to this article.

The full-form of JSON is JavaScript Object Notation. JSON is a lightweight data format for data interchange which can be easily read and written by humans, easily parsed and generated by machines. Though it is derived from a subset of JavaScript, yet it is Language independent. It is a complete language-independent text format. The following steps can be followed in order to publish JSON messages to Apache Kafka:

1.    Go to spring initializr and create a starter project with following dependencies:

·         Spring Web

·         Spring for Apache Kafka

2.    Open the project in an IDE and sync the dependencies. In this article, we would be creating a student model where we would be posting the student details. Therefore, create a model class Student. Add data members and create constructor and create getters and setters. The following is the implementation of the student class:

filter_none

brightness_4

// Java program to implement a

// student class

  

// Creating a student class

public class Student {

  

    // Data members of the

    // student class

    int id;

    String firstName;

    String lastName;

  

    // Constructor of the student

    // class

    public Student(int id, String firstName,

                   String lastName)

    {

        this.id = id;

        this.firstName = firstName;

        this.lastName = lastName;

    }

  

    // Implementing the getters

    // and setters

    public int getId()

    {

        return id;

    }

  

    public void setId(int id)

    {

        this.id = id;

    }

  

    public String getFirstName()

    {

        return firstName;

    }

  

    public void setFirstName(String firstName)

    {

        this.firstName = firstName;

    }

  

    public String getLastName()

    {

        return lastName;

    }

  

    public void setLastName(String lastName)

    {

        this.lastName = lastName;

    }

}


3.   

filter_none

brightness_4

// Java program to implement a

// controller

  

@RestController

@RequestMapping("gfg")

public class UserResource {

  

    @Autowired

    private KafkaTemplate<String, Student>

        kafkaTemplate;

  

    private static final String TOPIC

        = "StudentExample";

  

    @GetMapping("/publish/{id}/"

                + "{firstName}/{lastName}")

  

    public String post(

        @PathVariable("id") final int id,

        @PathVariable("firstName") final

            String firstName,

        @PathVariable("lastName") final

            String lastName)

    {

  

        kafkaTemplate.send(

            TOPIC,

            new Student(

                id, firstName,

                lastName));

  

        return "Published successfully";

    }

4.    Create a class StudentConfig with the annotation @Configuration. In this class we will serialize the object of the model class.

filter_none

brightness_4

// Java program to serialize the

// object of the model class

  

@Configuration

public class StudentConfig {

  

    @Bean

    public ProducerFactory<String, Student>

    producerFactory()

    {

        // Create a map of a string

        // and object

        Map<String, Object> config

            = new HashMap<>();

  

        config.put(

            ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,

            "127.0.0.1:9092");

  

        config.put(

            ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,

            StringSerializer.class);

  

        config.put(

            ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,

            JsonSerializer.class);

  

        return new DefaultKafkaProducerFactory<>(config);

    }

  

    @Bean

    public KafkaTemplate<String, Student>

    kafkaTemplate()

    {

        return new KafkaTemplate<>(

            producerFactory());

    }

5.   

6.   

For Mac and Linux: bin/kafka-topics.sh –create –zookeeper localhost:2181 –replication-factor 1 –partitions 1 –topic topic_name

For Windows: .\bin\windows\kafka-topics.bat –create –zookeeper localhost:2181 –replication-factor 1 –partitions 1 –topic topic_name

 

 

7.   

For Mac and Linux: bin/kafka-console-consumer.sh –bootstrap-server localhost:9092 –topic topic_name –from-beginning

For Windows: .\bin\windows\kafka-console-consumer.bat –bootstrap-server localhost:9092 –topic topic_name –from-beginning

8.   

localhost:8080/gfg/publish/{id}/{first name}/{last name}

Note:

Output: 

Calling the API:


Checking the message in real time:

3.

Ans-

In order to learn how to create a spring boot project, refer to 

The string is a sequence of characters. In java, objects of String are immutable which means a constant and cannot be changed once created. And also, java allows 

1.   

·        

·        

2.   

3.    In this class, create a GET API and initialize KafkaTemplate with parameter as string. The following is the implementation of the class:

filter_none

brightness_4

// Java program to implement the

// controller for the spring

// application

  

@RestController

@RequestMapping("/kafka")

public class Controller {

  

    @Autowired

    KafkaTemplate<String, String>

        kafkaTemplate;

  

    static final String TOPIC = "gfg";

  

    // Implementing a GET method

    @GetMapping("publish/{message}")

    public String publish_message(

        @PathVariable("message") String message)

    {

        kafkaTemplate.send(TOPIC, message);

        return "Message Published on Kafka !";

    }

}


4.   

5.   

For Mac and Linux: bin/kafka-topics.sh –create –zookeeper localhost:2181 –replication-factor 1 –partitions 1 –topic topic_name

For Windows: .\bin\windows\kafka-topics.bat –create –zookeeper localhost:2181 –replication-factor 1 –partitions 1 –topic topic_name

 

6.   

For MAC and Linux: bin/kafka-console-consumer.sh –bootstrap-server localhost:9092 –topic topic_name –from-beginning

For Windows: .\bin\windows\kafka-console-consumer.bat –bootstrap-server localhost:9092 –topic topic_name –from-beginning

7.   

localhost:8080/kafka/publish/{your message}

Note: 

Output:

·         Calling the API:

·         Checking the message in real time:

4.How to consume string messages using Apache Kafka in Spring boot.

Ans-

·        


Note: We can also create a maven project and add the following code to pom.xml file.

filter_none

brightness_4

<dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.kafka</groupId>

            <artifactId>spring-kafka</artifactId>

        </dependency>

  

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

            <exclusions>

                <exclusion>

                    <groupId>org.junit.vintage</groupId>

                    <artifactId>junit-vintage-engine</artifactId>

                </exclusion>

            </exclusions>

        </dependency>

        <dependency>

            <groupId>org.springframework.kafka</groupId>

            <artifactId>spring-kafka-test</artifactId>

            <scope>test</scope>

        </dependency>

    </dependencies>

  

    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

        </plugins>

    </build>

Step 2: Open the project in an IDE and sync the dependencies. Now create a new class Config and add annotations @Configuration and @EnableKafka.

Step 3: Now create 

beans ConsumerFactory and ConcurrentKafkaListenerContainerFactory with String object.

 Run the application and and type message on kafka producer and press enter.
Output:



Here type a message in string formate on kafka producer

>Hello

>Welcome to GeeksForGeeks

5.

Ans-

In this article, we will understand how to create a rest API using 

Spring

1.    Initially, we need to define the employee entity. Therefore, the following employee class is defined:

filter_none

brightness_4

package com.example.demo;

  

// Creating an entity Employee

public class Employee {

    public Employee() {}

    // Parameterized Constructor

    // to assign the values

    // to the properties of

    // the entity

     public Employee(

        Integer id, String firstName,

        String lastName, String email)

    {

        super();

        this.id = id;

        this.firstName = firstName;

        this.lastName = lastName;

        this.email = email;       

    }

       private Integer id;

       private String firstName;

       private String lastName;

       private String email;

    // Overriding the toString method

    // to find all the values

    @Override

   public String toString()

    {

        return "Employee [id="

            + id + ", firstName="

            + firstName + ", lastName="

            + lastName + ", email="

            + email + "]";        

    }

    // Getters and setters of

    // the properties

    public Integer getId()

    {

         return id;

    }

    public void setId(Integer id)

    {

         this.id = id;

    }

    public String getFirstName()

    {

         return firstName;

    }

    public void setFirstName(

        String firstName)

    {

        this.firstName = firstName;

    }

    public String getLastName()

    {

         return lastName;

    }

    public void setLastName(

        String lastName)

    {

         this.lastName = lastName;

    } 

    public String getEmail()

    {

         return email;

    }

    public void setEmail(String email)

    {

         this.email = email;

    }

2.    Now, we need to create a storage class which stores the list of all the employees:

filter_none

brightness_4

package com.example.demo;

  

import java.util.ArrayList;

import java.util.List;

  

// Class to store the list of

// all the employees in an

// Array List

public class Employees {

  

    private List<Employee> employeeList;

  

    // Method to return the list

    // of employees

    public List<Employee> getEmployeeList()

    {

  

        if (employeeList == null) {

  

            employeeList

                = new ArrayList<>();

  

                   

        }

  

        return employeeList;

  

           

    }

  

    public void

    setEmployeeList(

        List<Employee> employeeList)

    {

        this.employeeList

            = employeeList;

    }

3.    Till now, we have defined the entity employee and created a storage class. Now, we need to access the employees. So, we create a class from where we will create an object of the storage class to store the employees:

filter_none

brightness_4 

package com.example.demo;

  

import org.springframework

    .stereotype

    .Repository;

  

// Importing the employees class to

// use the defined properties

// in this class

import com.example.demo.Employees;

  

@Repository

  

// Class to create a list

// of employees

public class EmployeeDAO {

  

    private static Employees list

        = new Employees();

  

    // This static block is executed

    // before executing the main

    // block

    static

    {

  

        // Creating a few employees

        // and adding them to the list

        list.getEmployeeList().add(

            new Employee(

                1,

                "Prem",

                "Tiwari",

                "chapradreams@gmail.com"));

  

        list.getEmployeeList().add(

            new Employee(

                2, "Vikash",

                "Kumar",

                "abc@gmail.com"));

  

        list.getEmployeeList().add(

            new Employee(

                3, "Ritesh",

                "Ojha",

                "asdjf@gmail.com"));

  

           

    }

  

    // Method to return the list

    public Employees getAllEmployees()

    {

  

        return list;

    }

  

       

        // Method to add an employee

        // to the employees list

        public void

        addEmployee(Employee employee)

    {

        list.getEmployeeList()

            .add(employee);

           

    }

}

4.    Finally, we need to create a controller class which is the actual implementation of the REST API. According to the REST rules, every new entry in the database has to be called by the POST method and all the requests from the database must be called using the GET method. The same methods are implemented in the following code:

filter_none

brightness_4

package com.example.demo;

  

import java.net.URI;

import org.springframework.beans

    .factory.annotation.Autowired;

import org.springframework.http

    .ResponseEntity;

import org.springframework.web.bind

    .annotation.GetMapping;

import org.springframework.web.bind

    .annotation.PostMapping;

import org.springframework.web.bind

    .annotation.RequestBody;

import org.springframework.web.bind

    .annotation.RequestMapping;

import org.springframework.web.bind

    .annotation.RestController;

import org.springframework.web.servlet

    .support.ServletUriComponentsBuilder;

  

// Import the above-defined classes

// to use the properties of those

// classes

import com.example.demo.Employees;

import com.example.demo.EmployeeDAO;

import com.example.demo.Employee;

  

// Creating the REST controller

@RestController

@RequestMapping(path = "/employees")

public class EmployeeController {

  

    @Autowired

   private EmployeeDAO employeeDao;

       

        // Implementing a GET method

        // to get the list of all

        // the employees

   @GetMapping(

        path = "/",

        produces = "application/json")

  

    public Employees getEmployees()

    {

  

        return employeeDao

            .getAllEmployees();

    }

  

       

        // Create a POST method

        // to add an employee

        // to the list

   @PostMapping(

        path = "/",

        consumes = "application/json",

        produces = "application/json")

  

    public ResponseEntity<Object> addEmployee(

        @RequestBody Employee employee)

    {

  

        // Creating an ID of an employee

        // from the number of employees

        Integer id

            = employeeDao

                  .getAllEmployees()

                  .getEmployeeList()

                  .size()

              + 1;

  

        employee.setId(id);

  

        employeeDao

            .addEmployee(employee);

  

        URI location

            = ServletUriComponentsBuilder

                  .fromCurrentRequest()

                  .path("/{id}")

                  .buildAndExpand(

                      employee.getId())

                  .toUri();

  

               return ResponseEntity

            .created(location)

            .build();

    }

5.   

6. 

Output: The following is the output generated on running the above project: 

1.When a GET request is performed:

2.When a POST request is performed:

Again hitting the GET request after performing the POST request:

7. How to access database using Spring Data in Spring boot.

Spring Data JPA

Inorder to learn how to create a spring boot project, refer 

A database is a collection of inter-related data which helps in efficient retrieval, insertion and deletion of data from database and organizes the data in the form of tables, views, schemas, reports etc. So, for any application, database is one of the most important modules and there needs to be a way to communicate with it. Therefore, the following steps are followed in order to access the database using Spring Data JPA:

1.   

·        

·        

·        

2.   

3.    After the project syncs, we will create a model class Company with the annotation @Entity which means that this class is mapped to the table in the database. Add the data members with the data types the same as the columns in the database and generate constructor and getters. Add the annotation @Id to the data member which will behave as the primary key attribute in the table and @Generatedvalue(strategy = generationtype.auto) in order to auto increment the primary key attribute. The following is the implementation of this class:

filter_none

brightness_4

@Entity

public class Company {

  

    // Primary ID which increments

    // automatically when new entry

    // is added into the database

    @Id

    @GeneratedValue(strategy

                    = GenerationType.AUTO)

    int id;

  

    String name;

  

    // In months

    int duration;

    String profile;

  

    // Can be 0

    int stipend;

    boolean workFromHome;

  

    public Company()

    {

    }

  

    // Parameterized constructor

    public Company(String name, int duration,

                   String profile,

                   int stipend,

                   boolean workFromHome)

    {

        this.name = name;

        this.duration = duration;

        this.profile = profile;

        this.stipend = stipend;

        this.workFromHome = workFromHome;

    }

  

    // Getters and setters of

    // the variables

    public int getId()

    {

        return id;

    }

  

    public String getName()

    {

        return name;

    }

  

    public int getDuration()

    {

        return duration;

    }

  

    public String getProfile()

    {

        return profile;

    }

  

    public int getStipend()

    {

        return stipend;

    }

  

    public void setId(int id)

    {

        this.id = id;

    }

  

    public boolean isWorkFromHome()

    {

        return workFromHome;

    }

4.     CompanyRepository with the annotation @Repository which will implement the CrudRepository. The functions to perform the CRUD operations will be defined in the interface as shown below:

filter_none

brightness_4

@Repository

public interface CompanyRepository

    extends CrudRepository<Company,

                           Integer> {

  

    Company findById(int id);

    List<Company> findAll();

    void deleteById(int id);

Note:

5.    Now, we will create REST APIs(GET, POST, PUT, DELETE) as shown below:

filter_none

brightness_4 

@RestController

public class CompanyController {

    @Autowired

    private CompanyRepository repo;

  

    // Home Page

    @GetMapping("/")

    public String welcome()

    {

        return "<html><body>"

            + "<h1>WELCOME</h1>"

            + "</body></html>";

    }

  

    // Get All Notes

    @GetMapping("/company")

    public List<Company> getAllNotes()

    {

        return repo.findAll();

    }

  

    // Get the company details by

    // ID

    @GetMapping("/company/{id}")

    public Company getCompanyById(

        @PathVariable(value = "id") int id)

    {

        return repo.findById(id);

    }

  

    @PostMapping("/company")

    @ResponseStatus(HttpStatus.CREATED)

    public Company addCompany(

        @RequestBody Company company)

    {

        return repo.save(company);

    }

  

    @DeleteMapping("/delete/{id}")

    public void deleteStudent(

        @PathVariable(value = "id") int id)

    {

        repo.deleteById(id);

    }

  

    @PutMapping("/company/{id}")

    public ResponseEntity<Object> updateStudent(

        @RequestBody Company company,

        @PathVariable int id)

    {

  

        Optional<Company> companyRepo

            = Optional.ofNullable(

                repo.findById(id));

  

        if (!companyRepo.isPresent())

            return ResponseEntity

                .notFound()

                .build();

  

        company.setId(id);

  

        repo.save(company);

  

        return ResponseEntity

            .noContent()

            .build();

    }

6.   

spring.datasource.url=jdbc:mysql://localhost:3306/database_name

7.   

Note:

Output:

·        

·Testing with the POSTMAN collection:


Post a Comment

3 Comments

  1. Articles that are nice and very interesting I like to read the articles you make

    ReplyDelete
  2. Your blog got me to learn a lot,thanks for sharing,nice article

    ReplyDelete