Posts

Showing posts from March, 2021

How to create a Spring Boot Thymeleaf web application

Image
In this article, I will be demonstrating creating a Spring Boot Thymeleaf web application. Project Creation and Setup Step 1 – Create a new Maven Project  (Refer to  this  blog post). This should create a project as shown below: Step 2 – Add the Spring Boot and Thymeleaf dependency . So, the pom.xml  file should be similar to the following: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.learnjava</groupId> <artifactId>learnjava-springbootthymeleaf-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> </par

Python functions arguments with examples

In Part 1 of the Python functions article, I gave you an introduction to Python functions. In this article, we will take a look at Python functions with arguments. Arguments allow passing some data to a function so that the function then operates on the data. Python functions arguments are mainly of 4 types: positional, default, keyword, and arbitrary. Let us now take a look at each type of argument Positional Arguments Positional arguments are arguments whose values are passed in the same order in which they are specified in the function definition. Consider the following code: def showMarks(name,marks): print("Hello", name,"you have scored",marks,"marks!") This code specifies a showMarks function. It accepts arguments name and marks and simply prints a message with these values. You can now invoke this function as follows: showMarks("Jane",97) This assigns the value “Jane” to the first argument that is name and the value 97 to the seco

How to create a Spring Boot REST service with XML output

Image
In this article, I will be explaining how to create a Spring Boot REST service that produces an XML output. Edit   Project Creation and Setup   Step 1 – Create a new Maven Project  (Refer to  this blog post). This should create a project as shown below: Step 2 – Add the Spring Boot and Jackson dependency . So, your the pom.xml file should be similar to the following: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.learnjava</groupId> <artifactId>learnjava-springboot-xmloutput-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version&g

putIfAbsent and getOrDefault Map methods with examples

In my earlier article, I had explained the Map.compute, Map.computeIfPresent and Map.computeIfAbsent methods added by Java 8. In addition, Java 8 has added two other methods called putIfAbsent and getOrDefault to the Map interface.  In this article, I will be explaining the these methods. Edit How putIfAbsent works Prior to Java 8, the Map interface had a put method. This method accepts a key and a value as a parameter and adds the key-value pair to the Map. If the key passed to this method is present in the Map, its value gets overwritten with the new value. However, sometimes, we may wish to add the key-value pair only if the key is absent from the Map. The putIfAbsent method caters to such situations. Thus, it accepts a key and a value as a parameter and adds the key-value pair to the Map only if the specified key is not present in the Map. If the key is present in the Map, it does not do anything. The following code demonstrates the put and putIfAbsent methods: Map<

Difference between a local, instance and static variable in Java

Java supports three types of variables, local, instance and static. This post explains the difference between a local, instance and static variable in Java Local Variable Instance Variable Static Variable Defined within a method or a code block Defined outside a method at the class level Defined outside a method at the class level Is only accessible in the method/code block where it is declared Is accessible throughout the class Is accessible throughout the class Remains in memory as long as the method executes Remains in memory as long as the object is in memory Remains in memory as long as program executes Does not require any special keyword Does not require any special keyword but any access specifier (private, protected or public) can be specified. Typically, private or protected is used Requires the static keyword to be specified. In addition, any access specifier (private, protected or public) can be specified. Typically, public is used Requires to be initialized before it is us