gpt4 book ai didi

java - Spring Boot Web 应用程序无法在 Tomcat 上运行 - 404 错误

转载 作者:行者123 更新时间:2023-11-28 22:11:24 25 4
gpt4 key购买 nike

注意:也有与此类似的问题,但没有一种解决方案适合我。我花了几天时间试图让它发挥作用。

我已经将一个 WAR 文件部署到 Tomcat,并且“Tomcat Web 应用程序管理器”显示它已经启动。当我点击网络应用程序“/forecaster”时,出现 404 错误。从调试器 (IntelliJ) 运行时,Web 应用程序可以正常工作。 WAR 文件名为“forecaster.war”,我尝试过的路径是: http://localhost:8080/forecaster/ http://localhost:8080/forecaster/forecast/ http://localhost:8080/forecaster/forecast/1

“Catalina.sh 启动”输出:

Using CATALINA_BASE:   /Library/Tomecat 
Using CATALINA_HOME: /Library/Tomecat
Using CATALINA_TMPDIR: /Library/Tomecat/temp
Using JRE_HOME: /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home
Using CLASSPATH: /Library/Tomecat/bin/bootstrap.jar:/Library/Tomecat/bin/tomcat-juli.jar
Tomcat started.

构建.gradle:

buildscript {
ext {
springBootVersion = '1.4.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.springframework.boot:spring-boot-devtools:1.4.3.RELEASE")
classpath("org.springframework:spring-jdbc:4.3.5.RELEASE")
}
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'idea'
apply plugin: 'war'

jar {
baseName = 'forecaster'
version = '0.0.3-SNAPSHOT'
}

sourceCompatibility = 1.7
targetCompatibility = 1.7

repositories {
mavenCentral()
}

bootRun {
addResources = true
}

dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile("org.springframework.boot:spring-boot-starter-freemarker")
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-web-services')
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile("org.springframework:spring-jdbc")
compile("org.springframework.boot:spring-boot-devtools")
runtime('org.postgresql:postgresql')
compileOnly('org.projectlombok:lombok')
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testCompile('org.springframework.boot:spring-boot-starter-test')
}

预测应用程序.java:

package com.brian.project.forecaster;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.spring

framework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)
public class ForecasterApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ForecasterApplication.class);
}

public static void main(String[] args) {
SpringApplication.run(ForecasterApplication.class, args);
}
}

预测 Controller .java:

package com.brian.project.forecaster.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.brian.project.forecaster.services.DatabaseRepository;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ForecastController {

@Autowired
private DatabaseRepository databaseRepository;

@RequestMapping(value = "forecast/{id}")
public String getForecastForChain(@PathVariable Integer id, Model model) throws JsonProcessingException, IOException
{

model.addAttribute("futureForecastData", "Say 404 again I dare you...");
model.addAttribute("storeName", "test store name");

return "forecast";
}
}

最佳答案

我最终按照此处的说明进行操作,而不是使用 IntelliJ。

https://spring.io/guides/gs/spring-boot/#_run_the_application

在本地运行应用程序:

./gradlew build && java -jar build/libs/forecaster/forecaster-0.0.3-SNAPSHOT.jar

构建 war 我添加到 build.gradle 文件中:

war {
baseName = 'forecaster'
version = '0.0.3-SNAPSHOT'
}

然后我跑了:

./gradlew build

这在此处生成了 WAR 文件:

build/libs/forecaster/forecaster-0.0.3-SNAPSHOT.jar

然后我将其复制到外部 Tomcat8 服务器(其中/external-test/是安装到/Library/Tomcat/webapps 的已安装 samba 文件夹):

cp build/libs/forecaster/forecaster-0.0.3-SNAPSHOT.war /external-test/forecaster.war

现在它可以使用这个 URL:

http://localhost:8080/forecaster/forecast/1

关于java - Spring Boot Web 应用程序无法在 Tomcat 上运行 - 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41667372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com