首页 > 解决方案 > 如何将 Spring-MVC 连接到托管网站?

问题描述

我最近注册了一个免费网站,网址是http://kensinelli.infinityfreeapp.com。我正在尝试学习 Spring MVC,而不是在 localhost:8080 上做所有事情,我想在一个实际的网站上做所有事情,以便潜在的雇主可以轻松地看到我决定创建的任何内容。但是,我一直在努力弄清楚如何做到这一点。我用谷歌搜索了很多,发现了一些提到 application.properties 文件的资源,我设置了 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>

应用程序属性:

server.address=185.27.134.151
server.port=80

我知道我的代码此时没有做任何事情,但我现在只是想让它开始没有错误。我在这方面真的很新,所以请不要以为我什么都知道。一步一步的演练将不胜感激。请不要只说“阅读文档”,因为我已经看过它,要么我没有找到我正在寻找的东西,要么不理解它,所以我需要有人来澄清一下。谢谢你。

标签: htmlspringspring-mvcweb

解决方案


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


推荐阅读