SpringExamples

Spring boot change context path and server port

By Admin | Filed Under: Spring Boot

If you want to change the application context path (application root) and server port for any spring boot application, you can utilize any of below two methods:

1. application.properties file

Use server.port and server.contextPath properties.

#server port
server.port=9000

#application root
server.contextPath=/springexamples

2. Customize WebServerFactoryCustomizer

Implement WebServerFactoryCustomizer interface and override customize() method.

import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;

@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

	@Override
	public void customize(ConfigurableServletWebServerFactory server) {
		server.setPort(9000);
		server.setContextPath("/springexamples");
	}

}
Sourcecode Download

Ask Questions & Share Feedback Cancel reply

Your email address will not be published. Required fields are marked *

*Want to Post Code Snippets or XML content? Please use [java] ... [/java] tags otherwise code may not appear partially or even fully. e.g.
[java] 
public static void main (String[] args) {
...
}
[/java]

Development Environment

  • Spring Boot 2.0.1.RELEASE
  • Maven
  • JDK 8
  • Eclipse

Spring Boot Tutorial

  • Hello world example
  • Which main class
  • Print all beans
  • Embedded jetty server
  • CommandLineRunner
  • Custom banner
  • Change Root Path and Server Port

Spring Tutorial

  • IoC Container
  • Java Configuration

Spring WebMVC Tutorial

  • Minimum required config

Copyright © 2016 · springexamples.com · All Rights Reserved. | Sitemap