java.lang.IllegalArgumentException: Child name [/mytestapp] is not unique

Sometimes while deploying war to tomcat leads to the error. 15-Jun-2021 13:41:40.282 INFO [ajp-nio-127.0.0.1-8209-exec-10] org.apache.catalina.startup.HostConfig.undeploy Undeploying context [/mytestapp] 15-Jun-2021 13:41:40.956 INFO [ajp-nio-127.0.0.1-8209-exec-10] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/opt/tomcat/webapps/mytestapp.war] 15-Jun-2021 13:41:40.963 INFO [Catalina-utility-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/opt/tomcat/webapps/mytestapp.war] 15-Jun-2021 13:41:40.964 SEVERE [Catalina-utility-1] org.apache.catalina.startup.HostConfig.deployWAR Error deploying web application archive [/opt/tomcat/webapps/mytestapp.war] java.lang.IllegalArgumentException: Child name [/mytestapp] is not … Read more

java.lang.NoSuchMethodError: ‘org.junit.jupiter.api.extension.TestInstances org.junit.jupiter.api.extension.ExtensionContext.getRequiredTestInstances()’

Problem: java.lang.NoSuchMethodError: ‘org.junit.jupiter.api.extension.TestInstances org.junit.jupiter.api.extension.ExtensionContext.getRequiredTestInstances()’ Using : openJdk16 maven3 eclipse2021-03   Solution: I was using maven dependency for Junit 5 <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.3.0</version> <scope>test</scope> </dependency> And to solve this issue I had to update the dependency to <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.5.0</version> <scope>test</scope> </dependency>   After that change eclipse did run the tests fine.

Spring Boot Exception Handling

We have seen how spring boot helps us quickly put together RESTful web-services and web applications. When an application or service works as intended it leads to happy users and productivity. However, there are times when software may not be able to fulfill its functions – due to errors or exceptions. The causes of errors … Read more

Introduction to GraphQL with Spring Boot

This article is an introduction to GraphQL with spring boot. We will discuss basic concepts of GraphQL and setup a spring boot app for schema-driven development. What is GraphQL ? GraphQL is a data query language. It was created by Facebook. GraphQL provides a new API standard similar to REST. In REST API, the fundamental … Read more

Spring boot cheatsheet

Annotations Spring Boot and the rest of the Spring framework can be effectively configured using Annotations, starting from Java5. Below is a list of commonly used and/or powerful annotations to get up and running with Spring boot. Spring Framework Annotations Spring uses the below annotations to create and inject beans into the application context. These … Read more

Spring Boot Devtools

Spring Boot Devtools

Spring boot devtools has become a very popular dependency for rapid development and here’s why: Every developer wants to be productive and in many cases the speed with which the developer is able to move forward while adding features or fixing bugs is directly proportional to the frequency of the “edit-compile-run” cycle. Disciplines to create great code encourage developers to have really short cycle times in order of seconds.

Whether it’s developing RESTful API’s or web applications in a typical Java environment such as Eclipse or STS, each such cycle requires restarting the server, in order to make the server to pick up the changes. This is a cumbersome & wasteful process as restarting the server not only takes a lot of time depending on the size of the application but also requires developers to switch context making them less productive. You might have heard of the JRebel plugin which is a solution to this problem, but JRebel is not free depending on your use. What if there was a free tool to accomplish server restart and live-reload in a reliable and fast way ? This is where spring boot devtools comes in. By adding this to any Java Spring project, the developer can speed up the edit-compile-run cycle – the compile part is already addressed by STS or Eclipse and the run part is where devtools package helps.

Adding Spring Boot Devtools

To enable spring boot devtools package add it to your project’s dependencies. In Maven, for example, just add the spring-boot-devtools dependency to your pom.xml.

After saving pom.xml, maven will download the necessary dependencies in the background and include it in your project in the next compilation. If you are using STS, you can see these steps happening in the “Progress” tab.

Auto restart

There is an automatic restart of the applications which are using devtools, whenever a change occurs in the files of the classpath. Any entry on the classpath which points to a particular folder is monitored for changes, by default. The power of this feature is realised when there is an instant feedback for any change in the code, while working in any IDE like Eclipse or STS.

The Auto-restart feature automatically restarts the server by configuring and reloading the code in the server side, when there is a change in the source code. Thus, this feature eliminates the time to check if the changes are picked up by the server and thus enhances the speed of the development process. You can see auto-restart in action below.

Auto-Reload in Action
Auto-Reload in Action

Excluding resources

Some folders like /templates, /static, /public, /resources, /META–INF/maven, /META-INF/resources do not necessarily require a restart, when a change occurs. These can be customized by using spring.devtools.restart.exclude property.

For example, if you want to exclude the /static folder from monitoring for auto-reload, you can configure this exclusion, by setting this property in the application.properties file. For example,

spring.devtools.restart.exclude=static/**

With this setting, any change to files under /static will not trigger a restart, but will still trigger a live-reload. More on live-reload below.

Watch additional paths

It is also possible to restart when changes are made to files which are outside the classpath. This is achieved by setting the spring.devtools.restart.additional-paths property in application.properties file.

Fine-grained control

Apart from the above two, spring boot devtools provides one more property that would help trigger auto-reload on a single file. By setting spring.devtools.restart.trigger-file property to the path of the file that needs to trigger the restart, the server will auto restart every time this file changes on disk and will not restart when the classpath items change. For example,

spring.devtools.restart.trigger-file=MainApp.java

This provides for very fine-grained control to prevent too many restarts triggered by IDE auto-compliation during a typical edit cycle involving multiple files and the developer can trigger restart by modifying & saving this file. This triggers the restart check – a restart happens only if during this check spring boot devtools detects if something has changed.

Global devtools settings

Apart from customizing at project level by editing application.properties global devtools settings can be set in a file named .spring-boot-devtools.properties in the home directory of the user. Of course, the project-specific settings override global settings and global setting apply to all projects worked on by the user. This allows, for example, a developer to edit multiple projects and trigger a reload simultaneously after performing a consistent set of edits. By setting

spring.devtools.reload.trigger-file=.reloadtrigger

in ~/..spring-boot-devtools.properties the developer can run, e.g., touch ~/.spring-boot-devtools.properties anytime she wishes to trigger an auto-reload check. Again, only those projects where changes are detected will be restarted.

Live reload

Even if the application running on the server is auto-restarted, the developer would need to refresh the browser by manually clicking the refresh button when something changes while developing a web application. To circumvent this, Spring boot devtools provides integration to LiveReload by including an in-built embedded LiveReload Server, which automatically triggers the browser to refresh when a change is detected. This can be done by installing the LiveReload plugin on the browser. By installing this extension to the browser, the web-page gets automatically refreshed when there is a server restart or a change in the resources. These LiveReload browser extensions for Firefox, Chrome and Safari are available for free from livereload.com.

Note: Only one LiveReload server can run at a time.

Debugging Remote application with Spring Boot Devtools

The steps explained above are useful to debug a locally running application. But the same method can be extended to debug remote applications also. Spring Boot tunnels the debug data over HTTP and enables the developer to debug a JVM running remotely. To set this up, 3 additional steps are required:

  1. Ensure devtools is included in the repackaged archive. Check the following configuration is inclided in your pom.xml. Pay attention to the <configuration> tag.
  2. Add secret to authenticate the connection to the remote application.
    spring.devtools.remote.secret=<YOUR SECRET HERE>
    This is set once is application.properties and used both while building the application to be deployed remotely as well as by the debugger running locally when it tries to connect remotely.
  3. Deploy the application for the first time to remote URL. Repeat this step any time the value of secret is modified. Ensure the remote JVM is running in debug mode. The exact method to do this will depend on the remote deployment method. For example, if you have a dockerized web-app running on a remote server to which you are deploying, edit the dockerfile to launch java -Xdebug ... and rebuild the docker image if necessary.
  4. Add a new Run Configuration to your project with the same classpath as your project. Set the main class to org.springframework.boot.devtools.RemoteSpringApplication and provide the URL from step 3 as the argument. See docs for more details.

The application can then be debugged remotely using the newly added Run Configuration. See this video for an example with a web-app deployed in cloud-foundry.

With this setup Live Reload also works in the same method as explained above. Although the application is running remotely the LiveReload server runs locally and triggers the browser to refresh after the change has been pushed successfully.

Below is an example application.properties file that demostrates spring boot devtools configuration settings.

 

Trouble-shooting Spring Boot Devtools

Spring boot dev tools recommended environment set-up.

  1. Make sure you have Spring boot 1.3 or later to use the spring-boot-devtools dependency.

  2. Java 9 is not recommended for using spring boot. Java7/8 is preferred.

  3. If you are using Eclipse, make sure you have the right version of Eclipse installed. Recommended Eclipse Version

  • Oxygen
  • Eclipse Java EE version

Use case 1: Spring boot devtools not working in Eclipse

Check for the following:

  1. Ensure that the spring-boot-devtools dependency is added to your pom.xml:

  2. After adding the devtools dependency, clean and rebuild the project and also enable the build automatically. In Eclipse, in the top set the Project → “Build Automatically” to ON.

  3. Check if LiveReload plugin is installed in the browser which you are using.

  4. Clear the browser’s cache. This is done to make sure that your browser is requesting the new HTTP resources instead of using the cached vesion (especially those resources, which, by default do not need auto restart).
  5. Check if you have set the cache property of the template you are using to false, in order to disable the template’s cache. For example, to disable the cache of ‘thymeleaf’ template set the property as follow:
    spring.thymeleaf.cache = false

Use case 2: Spring boot Live Reload is not working in Intellij IDEA

Troubleshoot by following the below steps,

  1. Ensure that your pom.xml includes the ‘spring -boot-devtools’ dependency.

  2. Navigate to Settings → Build, Execution, Deployment → Compiler in your IntelliJ IDE and check the option ‘Make project automatically’

  3. Open the IntelliJ’s registry key by pressing SHIFT+CTRL+A. Type ‘registry’ on the window which is popped up and cheek the option ‘compiler.automake.allow.when.app.running’ by checking the check box.

  4. Restart your application to check if LiveReload is working in IntelliJ IDE.

 

Spring boot Actuator Example – Integration with rest service

What is Spring Boot Actuator ? Spring Boot Actuator is a spring feature which allows any web-app developer to add features to their web-services and applications to make them production-ready, such as monitoring and administration. The actuator is available as a library which attaches on-the-fly and provides tools to manage a web app by monitoring … Read more

Flyweight Design Pattern

Flyweight design pattern falls under the structural design pattern category. Sometimes, our applications hit slow performances. This can be due to various reasons. Instantiating many amounts of heavy objects can be one of the very common reasons for the low system performance. This can be rectified by the flyweight pattern successfully. Flyweight pattern introduces a … Read more

Java 8 – Lambda expressions

Java 8 Lambda expressions

Let us look at lambda expressions, which is one of the most important features introduced in java 8. Before we look at lambda expressions, let us first understand the functional interfaces and anonymous classes. What is functional interface? Functional interface means an interface that has exactly one abstract method. They have only one functionality to … Read more

REST API Interview Questions

Rest APi interview Questions

Here is a list of top 15 REST API interview questions and answers.  These questions would provide you with basic information about both simple and advanced topics. What is REST API ? REpresentational State Transfer, or REST for short, is an architectural style for building distributed, client-server systems. By providing a small set of useful … Read more