html - 如何将 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
我知道我的代码此时没有做任何事情,但我现在只是想让它开始没有错误。我在这方面真的很新,所以请不要以为我什么都知道。一步一步的演练将不胜感激。请不要只说“阅读文档”,因为我已经看过它,要么我没有找到我正在寻找的东西,要么不理解它,所以我需要有人来澄清一下。谢谢你。
解决方案
基本答案是您需要在他们的服务器上运行应用程序。您不能在本地运行它并让它为不同位置的请求提供服务。为此,您需要打包您的应用程序,将其上传到远程服务器,正确配置远程服务器的配置,并让远程服务器执行您打包的应用程序。您可能会发现使用 Heroku 之类的服务更容易。他们有很好的教程,并抽象出管理部署所涉及的一些复杂性。 https://devcenter.heroku.com/articles/deploying-spring-boot-apps-to-heroku
推荐阅读
- arrays - 使用@RequestParam在spring mvc中请求数组类型参数时如何始终采用第一个参数
- processing - 处理 3.4 中的翻转图像
- vue.js - 在 Vue 中按类名动态统计子元素
- c - 为什么当 recvfrom() 来自多播组地址时我得到 -1?
- python - (pymysql.err.OperationalError) (1045, u"Access denied for user 'cmr378'@'localhost' (using password: YES)")
- c# - 更新行时编辑文本文件
- powershell - 尝试从互联网机器运行 ps1 文件时,unblock-file 可以摆脱提示警告
- vba - 为什么我的 Dlookup 中有语法错误
- python - 数组是(800, )维,每个元素都是(240, )维,怎么改成(800, 240)
- php - 如果mysql不返回结果或为空,我将如何显示消息