简体   繁体   English

Spring Boot更改服务器端口

[英]Spring boot change server port

I have created Spring Maven project (using archetype maven-archetype-webapp) for web application. 我已经为Web应用程序创建了Spring Maven项目(使用原型maven-archetype-webapp)。 I need to bind on ip different from localhost and different port. 我需要在不同于localhost和不同端口的ip上进行绑定。 I have created file "application.properties" in resources folder and added following lines: 我在资源文件夹中创建了文件“ application.properties”,并添加了以下几行:

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. 但是,在启动时,它仍然使用默认端口8080,并且ip仍然是localhost。

My WebInitializer class is : 我的WebInitializer类是:

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? 我究竟做错了什么?

No, you can't change the server port unless you are using an embedded servlet container ie, if you are deploying your web application (war) directly into Tomcat, then changing the port number in application.properties will not simply work. 不,除非您使用嵌入式Servlet容器,否则您无法更改服务器端口,即,如果将Web应用程序(war)直接部署到Tomcat中,那么更改application.properties的端口号将不起作用。 For this, you need to change the port in Tomcat server's server.xml . 为此,您需要更改Tomcat服务器的server.xmlport Also, if you wanted to configure the Tomcat server IP address then, you can look here . 另外,如果您想配置Tomcat服务器IP地址,则可以在此处查看

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. 您可以在此处查看如何托管嵌入式servlet容器,以便可以使用application.properties来配置IP和端口详细信息。

Actually the easiest way is to change Tomcat setting by server.xml file and there change port 实际上,最简单的方法是通过server.xml文件更改Tomcat设置,然后更改端口

<Connector connectionTimeout="20000" port="8000" protocol="HTTP/1.1" redirectPort="8443"/>

To change IP it is enough to using Spring click on Tomcat properties and change Host name to local IP 要更改IP,足以使用Spring单击Tomcat属性并将“主机名”更改为本地IP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM