Tuesday, 27 August 2019

Configure Context path of spring boot application


Spring boot serves the content on root context path (/) by default.

If you want to change the context path of spring boot application, you should add below property in application.properties file.
server.servlet.contextPath=/{newContextPath}

Example
server.servlet.contextPath=/dempApp

Find the below working application.

application.properties
server.servlet.contextPath=/dempApp


HomeController.java
package com.sample.app.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

	@RequestMapping("/")
	public String homePage() {
		return "Welcome to Spring boot Application Development";
	}

}


App.java
package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
	public static void main(String[] args) {

		SpringApplication.run(App.class, args);

	}

}


pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>springbootMisc</groupId>
	<artifactId>springbootMisc</artifactId>
	<version>1</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
	</parent>

	<dependencies>

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

	</dependencies>
</project>


Total project structure looks like below.

Run App.java.

Open the url ‘http://localhost:8080/dempApp/’ in browser, you can see below kind of screen.
You can download the complete working application from this link.


You can even set the servlet context in below ways.

a. Using System.setProperty
System.setProperty("server.servlet.context-path", "/dempApp");


App.java
package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
	public static void main(String[] args) {

		System.setProperty("server.servlet.context-path", "/dempApp");
		SpringApplication.run(App.class, args);

	}

}

b. By passing the vm argument '-Dserver.servlet.context-path=/demoApp'

Right click on App.java.

Run As -> Run Configurations.


Under VM arguments: section add below argument.
-Dserver.servlet.context-path=/demoApp

c. Using a configuration bean.
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() {
         return factory -> factory.setContextPath("/demoApp");
}

App.java

package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.*;

@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);

	}

	@Bean
	public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() {
		return factory -> factory.setContextPath("/demoApp");
	}

}



Previous                                                    Next                                                    Home

No comments:

Post a Comment