Spring Boot Change PortThe Spring Boot framework provides the default embedded server (Tomcat) to run the Spring Boot application. It runs on port 8080. It is possible to change the port in Spring Boot. We can change the port in Spring Boot by using the following interfaces and properties files:
Using application.properties fileWe recommend you to use the application.properties file if you want to change the default port. Because it is an easy and faster way to overwrite default values. We use the server.port property to overwrite the default property. For example, if we want to change default port 8080 to 8082, specify the property in application.properties file. application.properties We can also set the port property to 0. It scans the random port for the application. It uses a new port whenever we restart our application. application.properties Using application.yml fileSimilarly, we can also change the default port by using a yml file. Use either application.properties or application.yml file, both the files works in the same manner. application.yml Using EmbeddedServletContainerCustomizer InterfaceIf you are using Spring Boot 1.x version, it provides an interface EmbeddedServletContainerCustomizer to change the default port. EmbeddedServletContainerCustomizer Interface By using the EmbeddedServletContainerCustomizer, we can customize auto-configured embedded servlet containers. All the beans of this type get a callback with the container factory before starting the container itself. Therefore, we can set the port, addresses, and even error pages. It is defined in the org.springframework.boot.context.embedded package. The interface contains a method called customize(). It allows us to customize and specify ConfigurableEmbeddedServletContainer. It parses a parameter called container that we want to customize. SyntaxConfigurableEmbeddedServletContainer Interface It is an interface that reflects the changes in the EmbeddedServletContainerFactory interface (factory interface used to create EmbeddedServletContainers). It is defined in the org.springframework.boot.context.embedded package. It contains a method to change the port called the setPort() method. setPort() method The setPort() method configures the port of embedded servlet container should listen on. When we do not specify the port, it uses the default port 8080. If we want to disable the auto-start feature of the embedded server, use the port -1. The port -1 represents that it will not listen to any port but start the web application context. The method parses a parameter port (the port to set) of type int. SyntaxIn the following example, we have created a class named ServerCustomizer and implements the EmbeddedServletContainerCustomizer Interface. We have overridden the customize() method and invoke the setPort() method that sets the port 8097. ServerCustomizer.java Using WebServerFactoryCustomizer InterfaceSpring Boot 2.x version provides WebServerFactoryCustomizer interface to change the default port. It defined in the package org.springframework.boot.web.server. It parses a parameter T of type web server factory. SyntaxThe interface contains a method called customize(). It allows us to customize web server factories. It parses a parameter called factory that we want to customize. All the beans of this type get a callback with the server factory before starting the container itself. Therefore, we can set the port, addresses, and even error pages. SyntaxWebServerFactory Interface It is a tagging interface for factories. It is defined in org.springframework.boot.web.server package. It creates a WebServer. ConfigurableWebServerFactory It is an interface that configures web server factory. It is defined in the package org.springframework.boot.web.server. It extends WebServerFactory and ErrorPageRegistory. It contains a method to change the port called the setPort() method. setPort() The setPort() method configures the port of embedded servlet container should listen on. When we do not specify the port, it uses the default port 8080. If we want to disable the auto-start feature of the embedded server, use the port -1. The port -1 represents that it will not listen to any port but start the web application context. The method parses a parameter port (the port to set) of type int. SyntaxNote: This setPort() method is of interface ConfigurableWebServerFactoryIn the following example, we have created a class named ServerCustomizer that implements the WebServerFactoryCustomizer Interface. We have overridden the customize() method and invoke the setPort() method that sets the port 9001. ServerCustomizer.java Using Command Line ParameterWe can also change the port in Spring Boot by using the command line parameter. We must follow the steps given below:
Next TopicSpring
|
Python tutorial provides basic and advanced concepts of Python.
Vue.js is an open-source progressive JavaScript framework
HTML refers to Hypertext Markup Language. HTML is the gateway ...
Java is an object-oriented, class-based computer-programming language.
PHP is an open-source,interpreted scripting language.
Spring is a lightweight framework.Spring framework makes ...
JavaScript is an scripting language which is lightweight and cross-platform.
CSS refers to Cascading Style Sheets...
jQuery is a small and lightweight JavaScript library. jQuery ...
SQL is used to perform operations on the records stored in the database.
C programming is considered as the base for other programming languages.
JavaScript is an scripting language which is lightweight and cross-platform.
Vue.js is an open-source progressive JavaScript framework
ReactJS is a declarative, efficient, and flexible JavaScript library.
jQuery is a small and lightweight JavaScript library. jQuery ...
Node.js is a cross-platform environment and library for running JavaScript app...
TypeScript is a strongly typed superset of JavaScript which compiles to plain JavaScript.
Angular JS is an open source JavaScript framework by Google to build web app...
JSON is lightweight data-interchange format.
AJAX is an acronym for Asynchronous JavaScript and XML.
ES6 or ECMAScript 6 is a scripting language specification ...
Angular 7 is completely based on components.
jQuery UI is a set of user interface interactions built on jQuery...
Python tutorial provides basic and advanced concepts of Python.
Java is an object-oriented, class-based computer-programming language.
Node.js is a cross-platform environment and library for running JavaScript app...
PHP is an open-source,interpreted scripting language.
Go is a programming language which is developed by Google...
C programming is considered as the base for other programming languages.
C++ is an object-oriented programming language. It is an extension to C programming.
C# is a programming language of .Net Framework.
Ruby is an open-source and fully object-oriented programming language.
JSP technology is used to create web application just like Servlet technology.
The JSTL represents a set of tags to simplify the JSP development.
ASP.NET is a web framework designed and developed by Microsoft.
Perl is a cross-platform environment and library for running JavaScript...
Scala is an object-oriented and functional programming language.
VBA stands for Visual Basic for Applications.
Spring is a lightweight framework.Spring framework makes ...
Spring Boot is a Spring module that provides the RAD feature...
Django is a Web Application Framework which is used to develop web applications.
Servlet technology is robust and scalable because of java language.
The Struts 2 framework is used to develop MVC based web applications.
Hibernate is an open source, lightweight, ORM tool.
Solr is a scalable, ready-to-deploy enterprise search engine.
SQL is used to perform operations on the records stored in the database.
MySQL is a relational database management system based...
Oracle is a relational database management system.
SQL Server is software developed by Microsoft.
PostgreSQL is an ORDBMS.
DB2 is a database server developed by IBM.
Redis is a No SQL database which works on the concept of key-value pair.
SQLite is embedded relational database management system.
MongoDB is a No SQL database. It is an document-oriented database...
Memcached is a free, distributed memory object caching system.
Hibernate is an open source, lightweight, ORM tool.
PL/SQL is a block structured language that can have multiple blocks in it.
DBMS Tutorial is software that is used to manage the database.
Spark is a unified analytics engine for large-scale data processing...
IntelliJ IDEA is an IDE for Java Developers which is developed by...
Git is a modern and widely used distributed version control system in the world.
GitHub is an immense platform for code hosting.
SVN is an open-source centralized version control system.
Maven is a powerful project management tool that is based on POM.
Jsoup is a java html parser.
UML is a general-purpose, graphical modeling language.
RESTful Web Services are REST Architecture based Web Services.
Postman is one testing tools which is used for API testing.
JMeter is to analyze the performance of web application.
Jenkins builds and tests our software projects.
SEO stands for Search Engine Optimization.
MATLAB is a software package for mathematical computation, visualization...
Unity is an engine for creating games on multiple platforms.
Hadoop is an open source framework.
Pig is a high-level data flow platform for executing Map Reduce programs of Hadoop.
Spark is a unified analytics engine for large-scale data processing...
Spring Cloud is a framework for building robust cloud applications.
Spring Boot is a Spring module that provides the RAD feature...
AI is one of the fascinating and universal fields of Computer.
Cloud computing is a virtualization-based technology.
AWS stands for Amazon Web Services which uses distributed IT...
Microsoft Azure is a cloud computing platform...
IoT stands for Internet of Things...
Spring Cloud is a framework for building robust cloud applications.
Email:jjw.quan@gmail.com