Search code examples
javaspringmavenspring-mvcspring-web

Spring boot change server port


I have created Spring Maven project (using archetype maven-archetype-webapp) for web application. I need to bind on ip different from localhost and different port. I have created file "application.properties" in resources folder and added following lines:

server.port=8001
server.address= 192.168.1.91

However on startup it still uses port default one 8080 and also ip is still localhost.

My WebInitializer class is :

package guard;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"*.html"};
    }

}

What am I doing wrong?


Solution

  • No, you can't change the server port unless you are using an embedded servlet container i.e, if you are deploying your web application (war) directly into Tomcat, then changing the port number in application.properties will not simply work. For this, you need to change the port in Tomcat server's server.xml. Also, if you wanted to configure the Tomcat server IP address then, you can look here.

    You can look here on how embedded servlet containers can be hosted so that you can use application.properties to configure IP and port details.