Spring Boot Web 应用程序无法在 Tomcat 上运行 - 404 错误 [英] Spring boot web app not working on Tomcat - 404 error

查看:49
本文介绍了Spring Boot Web 应用程序无法在 Tomcat 上运行 - 404 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Note: There are similar questions to this one but none of the solutions work for me. I've spent a few days trying to get this to work.

我已将 WAR 文件部署到 Tomcat,Tomcat Web 应用程序管理器"显示它已启动.当我单击网络应用程序/forecaster"时,出现 404 错误.Web 应用程序在从调试器 (IntelliJ) 运行时工作.WAR 文件名为forecaster.war",我尝试过的路径是:http://localhost:8080/forecaster/http://localhost:8080/forecaster/forecast/http://localhost:8080/forecaster/forecast/1

I've deployed a WAR file to Tomcat and the "Tomcat Web Application Manager" shows that it has started. When I click on the web app "/forecaster" I get a 404 error. The web app works when running from the debugger (IntelliJ). The WAR file is named "forecaster.war" and the paths I've tried are: http://localhost:8080/forecaster/ http://localhost:8080/forecaster/forecast/ http://localhost:8080/forecaster/forecast/1

Catalina.sh start"输出:

"Catalina.sh start" output:

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.

Build.gradle:

Build.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')
}

ForecasterApplication.java:

ForecasterApplication.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;

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);
    }
}

ForecastController.java:

ForecastController.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.

I ended up following the instructions here instead of using IntelliJ.

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

在本地运行应用程序:

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

为了构建我添加到 build.gradle 文件中的战争:

To build the war I added to the build.gradle file:

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

然后我跑了:

./gradlew build

这里生成了 WAR 文件:

This produced the WAR file here:

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

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

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

And I copied that to the external Tomcat8 server (where /external-test/ is the mounted samba folder mounted to /Library/Tomcat/webapps):

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

现在它适用于这个 URL:

Now it works with this URL:

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

这篇关于Spring Boot Web 应用程序无法在 Tomcat 上运行 - 404 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆