Change Server Port in Spring Boot

1. Overview

Typically, the Spring Boot apps run on the port 8080. However, in some cases we may need to run the apps on other ports. We can change the server port easily in Spring Boot.

2. Using Properties

Let’s change the server port using server.port property in our application properties:

server.port = 9000

In case, we are using yml, then we can change the port in application.yml properties file using server port property:

server:
  port: 9001

3. Using Command Line

We can also configure the port while launching the app. In this case, we don’t need to modify the application properties or the code.

Now, let’s provide the server port while launching the application using the cmd:

java -Dserver.port=9001 -jar SpringBootDemo.jar

Or, we can pass server port as an argument to the Spring boot application:

java -jar SpringBootDemo.jar –-server.port=9001

4. Changing Port Programmatically

Now, let’s change the server port programmatically by extending WebServerFactoryCustomizer interface:

@Component
public class ServerCustomization implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory > {

	public void customize(ConfigurableServletWebServerFactory server) {
		server.setPort(9002);
	}
}

5. Conclusion

In this article, we discussed various options to change the server port for Spring Boot apps.

As always the code for all these examples is available on GitHub.

Installing Java 8 on Ubuntu

 

First check if Java is already installed on the system. Run the below command to check if Java is already installed on the system.

 

java –version

 

If java is not installed on the system, you should see message similar to below.

1

 

If the java is already installed on the system, then it should display the version of java installed on the system.

 

11

 

If java is not installed on the system, then run the below command to start java installation.

sudo add-apt-repository ppa:webupd8team/java

Press [ENTER] to add the java repository information to the system.

2

3

 

Once java repository information is added, run the below command to update the package lists since java package is added to the list earlier.

 

sudo apt update

 

4

 

Run the below command to download and install java 8.

 

sudo apt install oracle-java8-installer

 

Enter ‘Y’ when the installer asks for confirmation to start java installation.

5

 

Select ‘Ok’, when the installer asks for accepting the java license.

 

6

 

Select ‘Yes’ when the  installer asks for accepting the Oracle license.

 

7

 

The download and installation of Java will take some time.

8

9

10

 

Run the below command to set the java version we have installed as the default version.

 

sudo apt install oracle-java8-set-default

 

12

Once the installation of java is complete, run the java version command again to check the java version.

 

java –version

 

11

 

 

Conclusion

 

java 8 (including JDK) is installed on Ubuntu and can be used for developing or running java applications.

Fixing FileUploadBase$SizeLimitExceededException on Tomcat.

 

Mostly when doing hot deployments of web applications through manager on Tomcat, the hot deployment may fail with reason similar to below.

 

SEVERE [http-nio-8080-exec-2] org.apache.catalina.core.ApplicationContext.log HTMLManager: FAIL – Deploy Upload Failed, Exception: [org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (75715094) exceeds the configured maximum (52428800)]
java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (75715094) exceeds the configured maximum (52428800)
    at org.apache.catalina.connector.Request.parseParts(Request.java:2890)
    at org.apache.catalina.connector.Request.parseParameters(Request.java:3185)
    at org.apache.catalina.connector.Request.getParameter(Request.java:1116)
    at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381)
    at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:109)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:607)
    at org.apache.catalina.valves.RequestFilterValve.process(RequestFilterValve.java:348)
    at org.apache.catalina.valves.RemoteAddrValve.invoke(RemoteAddrValve.java:52)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (75715094) exceeds the configured maximum (52428800)
    at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:805)
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:256)
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:280)
    at org.apache.catalina.connector.Request.parseParts(Request.java:2846)
    … 28 more

 

This is because the max file size that can be uploaded through manager app is set to 50MB, and your war file size is larger than the 50 MB.

This limit can be modified in web.xml file available at <Your Tomcat Root Folder>\webapps\manager\WEB-INF. If you open the web.xml, you should find configurations similar to below.

 

<servlet>
    <servlet-name>HTMLManager</servlet-name>
    <servlet-class>org.apache.catalina.manager.HTMLManagerServlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>

  <!– Uncomment this to show proxy sessions from the Backup manager or a
       StoreManager in the sessions list for an application
  <init-param>
    <param-name>showProxySessions</param-name>
    <param-value>true</param-value>
  </init-param>
  –>
    <multipart-config>
      <!– 50MB max –>
      <max-file-size>52428800</max-file-size>
      <max-request-size>52428800</max-request-size>

      <file-size-threshold>0</file-size-threshold>
    </multipart-config>
  </servlet>

 

Change the max-file-size and max-request-size to a larger value. The value is in Bytes, so convert the value you want to configure into bytes. So if you want to configure 100 MB, then the value should be 100*1024*104 = 104857600. So the new configuration will look similar to below.

 

<servlet>
  <servlet-name>HTMLManager</servlet-name>
  <servlet-class>org.apache.catalina.manager.HTMLManagerServlet</servlet-class>
  <init-param>
    <param-name>debug</param-name>
    <param-value>2</param-value>
  </init-param>
  <!– Uncomment this to show proxy sessions from the Backup manager or a
       StoreManager in the sessions list for an application
  <init-param>
    <param-name>showProxySessions</param-name>
    <param-value>true</param-value>
  </init-param>
  –>
  <multipart-config>
    <!– 100MB max –>
    <max-file-size>104857600</max-file-size>
    <max-request-size>104857600</max-request-size>

    <file-size-threshold>0</file-size-threshold>
  </multipart-config>
</servlet>

Once the modification is saved, restart the tomcat for the settings to apply and then should be able to deploy a war file up to 100 MB without any issues. The value can be changed based on the size of war files that you normally deploy.