gpt4 book ai didi

java - 初始 Spring Boot 启动项目设置 - 面临的问题

转载 作者:行者123 更新时间:2023-12-01 19:51:43 25 4
gpt4 key购买 nike

我正在尝试使用提供的 Spring boot 入门包制作一个简单的 Spring Web 应用程序。我能够在控制台中显示“Hello World”,但是当我尝试打开 localhost:(port) 时,它向我显示 .我正在使用 JRE 8。尝试使用 JDK8 但也给了我同样的错误。我们必须使用 JDK 还是 JRE 吗?

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Mon Jul 02 21:58:01 MDT 2018 There was an unexpected error (type=Not Found, status=404). No message available

我试图解决过去两天的这个问题,但我陷入了困境!。

请找到我系统中的以下图像和代码。

Project Structure

Spring Starter 项目应用程序 .java

package com.gami.mvc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringStarterProjectApplication extends SpringBootServletInitializer{

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringStarterProjectApplication.class);
}

public static void main(String[] args) {
SpringApplication.run(SpringStarterProjectApplication.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.gami.mvc</groupId>
<artifactId>springStarterProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springStarterProject</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<!-- This is a web application -->

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Tomcat embedded container -->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>


<!-- JSTL for JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

<!-- Need this to compile JSP -->

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>

<!-- Optional, test for static content, bootstrap CSS -->

<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>

</dependencies>

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


</project>

Controller 。 java

package com.gami.mvc;

import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;

public class WelcomeController {

// inject via application.properties
@Value("${welcome.message:test}")
private String message = "Hello World";

@RequestMapping("/")
public String welcome(Map<String, Object> model) {
model.put("message", this.message);
return "welcome";

}
}

应用程序属性

server.port=8456

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

welcome.message: Hello Gami

运行应用程序时接收错误

Error Message

请提供任何帮助,非常感谢!

谢谢哈迪克·加米 Console Image

Console msg after hitting to local host

Java Build Path Error

Java 构建路径 - Eclipse Java Build Path of Project

最佳答案

Spring 将在配置的搜索路径上搜索带有 @Controller 注解的类,如果没有此注解,Spring 将不会获取您的 Controller 类。

@Controller
public class WelcomeController { ... }

关于java - 初始 Spring Boot 启动项目设置 - 面临的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51146569/

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