如何将 Spring-MVC 连接到托管网站?

How do I connect Spring-MVC to a hosted website?

我最近注册了一个免费网站,URL 是 http://kensinelli.infinityfreeapp.com. I'm trying to learn Spring MVC, and rather than do everything on localhost:8080, I wanted to do everything on an actual website so that potential employers can easily see whatever I decide to create. However, I've been struggling to figure out how to accomplish this. I've Google'd quite a bit and found some resources mentioning the application.properties file, and I've set server.address = http://kensinelli.infinityfreeapp.com 和 server.port = 80。我也试过设置 server.address = 185.27.134.151这是网站控制面板中规定的IP地址。当我使用 IP 地址并尝试启动 Spring 时,出现错误:

org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat server

当我使用 http://kensinelli.infinityfreeapp.com 而不是 IP 地址时,出现此错误:

Failed to bind properties under 'server.address' to java.net.InetAddress:

    Property: server.address
    Value: http://kensinelli.infinityfreeapp.com
    Origin: class path resource [application.properties] - 1:16
    Reason: failed to convert java.lang.String to java.net.InetAddress

Action:

Update your application's configuration

所以我认为 server.address 应该是一个实际的 IP 地址,而不是通过 DNS 运行 的命名服务器地址。

但是我什至需要通过 Spring 的内置 Tomcat 来做到这一点吗?我能以某种方式绕过它吗?或者是否需要 Tomcat 才能连接到外部网站?

我的文件目前是这样的:

package spring.project;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebProjectApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebProjectApplication.class, args);
        
        System.out.println("Hello world");
    }

}

pom.xml(一些依赖项被注释掉了,因为我计划在未来使用它们,但它们现在导致我在启动时出错):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>spring.project</groupId>
    <artifactId>webProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>webProject</name>
    <description>spring project</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
<!--        <dependency> -->
<!--            <groupId>org.springframework.boot</groupId> -->
<!--            <artifactId>spring-boot-starter-jdbc</artifactId> -->
<!--        </dependency> -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
<!--        <dependency> -->
<!--            <groupId>org.springframework.boot</groupId> -->
<!--            <artifactId>spring-boot-starter-thymeleaf</artifactId> -->
<!--        </dependency> -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<!--        <dependency> -->
<!--            <groupId>org.thymeleaf.extras</groupId> -->
<!--            <artifactId>thymeleaf-extras-springsecurity5</artifactId> -->
<!--        </dependency> -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
<!--        <dependency> -->
<!--            <groupId>org.postgresql</groupId> -->
<!--            <artifactId>postgresql</artifactId> -->
<!--            <scope>runtime</scope> -->
<!--        </dependency> -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties:

server.address=185.27.134.151
server.port=80

我知道我的代码此时没有执行任何操作,但我现在只是想让它无错误地启动。我在这方面真的很陌生,所以请不要以为我什么都知道。一步一步的演练将不胜感激。请不要只说“阅读文档”,因为我已经看过它了,要么我没有找到我要找的东西,要么我不理解它,所以我需要有人来澄清。谢谢。

基本答案是您需要 运行 他们服务器上的应用程序。您不能 运行 它在本地并让它为不同位置的请求提供服务。为此,您需要打包您的应用程序,将其上传到远程服务器,并为远程服务器正确配置配置,并让远程服务器执行您打包的应用程序。您可能会发现使用像 Heroku 这样的服务更容易。他们有很好的教程,并抽象出管理部署所涉及的一些复杂性。 https://devcenter.heroku.com/articles/deploying-spring-boot-apps-to-heroku