【问题标题】:SpringBoot - HelloWorldSpring Boot - HelloWorld
【发布时间】:2018-12-22 17:04:25
【问题描述】:

我想用 SpringBoot 创建一个简单的 hello world 应用程序,其中 localhost:8080/welcome.html 将向我们显示 Hello World。

我认为我做得很好,但我看不到 HelloWorld,只是 Whitelabel 错误页面。

这是我的仓库的链接。如果有人可以检查出什么问题,我会很高兴! https://github.com/BElluu/ElenXHello

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.springbelluu</groupId>
    <artifactId>springboot-helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-helloworld</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.BUILD-SNAPSHOT</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>10</java.version>
    </properties>

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

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

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

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

    </dependencies>

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

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>


</project>

SpringbootHelloworldApplication.java

package com.springbelluu.springboothelloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootHelloworldApplication {

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

TestController.java

package com.javainuse.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class TestController {

    @RequestMapping("/welcome.html")
    public ModelAndView firstPage() {
        return new ModelAndView("welcome");
    }

}

application.properties

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

【问题讨论】:

  • 在问题本身中发布您的代码。
  • "寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题中重现它所需的最短代码本身。没有明确问题陈述的问题对其他读者没有用处。"
  • 我不确定这是否是您的问题,但您是否尝试过将端点设置为“/welcome”而不是“/welcome.html”。我不是 Spring Boot 方面的专家,但我不确定您是否真的可以映射到 *.html
  • 这是因为您的应用程序的包结构。你的控制器在一个完全不同的包com.javainuse.controllers 中,而不是你在包com.springbelluu.springboothelloworld 中的应用程序类。确保控制器在应用程序类的同一个包或子包中,否则 Spring 将找不到它。或者在引用包含控制器的包的应用程序类中添加@ComponentScan() 注解。

标签: java spring spring-boot


【解决方案1】:

对于初学者,如果您想使用 Spring Boot 入门,我强烈建议 使用 JSP。使用 JSP 时有很多 some limitations,其中之一是它不适用于 jar 包装。其次,它是一项过时的技术,除了在较新的 JEE 版本中保持功能之外,不再受到太多关注/更新。最好使用Thymeleaf之类的东西。

接下来,您将使用 Spring Boot 版本的快照版本,该版本已经位于 2.1.3.RELEASE(在撰写本文时)。

话虽如此,请将您的pom.xml 更改为以下内容(修复版本,删除 JSP 内容并替换为 Thymeleaf)。

<?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.springbelluu</groupId>
    <artifactId>springboot-helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-helloworld</name>
    <description>Demo project for Spring Boot</description>

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

    <properties>
        <java.version>10</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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-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>

注意:因为您现在使用的是最终版本,所以您不再需要 pom.xml 中的所有存储库!。

现在删除您的 JSP 并在 src/main/resources/templates/ 中创建一个 welcome.html。 (您实际上可以完全删除您的 webapp 目录。

<html>
<body>
<h1>Welcome! Spring Boot for ElenX</h1>
</body>
</html>

您现在拥有的设置比 JSP 更现代且更易于使用。

在您的 application.properties 中删除 spring.mvc.view 属性,因为 Spring Boot 将自动使用正确的设置配置 Thymeleaf。

【讨论】:

    【解决方案2】:

    @BElluu ...您的主应用程序和 Controller 类位于不同的包中,因此 componentscan 无法正常工作...只需检查包级别

    package com.springbelluu.springboothelloworld;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class SpringbootHelloworldApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootHelloworldApplication.class, args);
        }
    }
    
    
    package com.javainuse.controllers;  
     // use this package package com.springbelluu.springboothelloworld;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class TestController {
    
        @RequestMapping("/welcome.html")
        public ModelAndView firstPage() {
            return new ModelAndView("welcome");
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      通过 Spring Framework 转到以下链接以创建全新的 Springboot 项目:

      https://start.spring.io/

      1.创建一个简单的网络应用程序

      现在您可以为简单的 Web 应用程序创建一个 Web 控制器。

      src/main/java/hello/HelloController.java

      打包你好;

      导入 org.springframework.web.bind.annotation.RestController; 导入 org.springframework.web.bind.annotation.RequestMapping;

      @RestController 公共类 HelloController {

      @RequestMapping("/")
      public String index() {
          return "Greetings from Spring Boot!";
      }
      

      }

      1. 创建应用程序类

        Here you create an Application class with the components:
        
        src/main/java/hello/Application.java
        
         package hello;
        
         import java.util.Arrays;
         import org.springframework.boot.CommandLineRunner;
         import org.springframework.boot.SpringApplication;
         import org.springframework.boot.autoconfigure.SpringBootApplication;
         import org.springframework.context.ApplicationContext;
         import org.springframework.context.annotation.Bean;
        
         @SpringBootApplication
         public class Application {
        
         public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
          }
        
          @Bean
          public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
             return args -> {
            System.out.println("Let's inspect the beans provided by Spring Boot:");
        
            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
                for (String beanName : beanNames) {
                    System.out.println(beanName);
                 }
              };
            }
          }
        

      【讨论】:

        【解决方案4】:
           Here is SpringBoot Main class file Noting Changed Here.  
            -----------------------------------------
        
            package com.Encee.SpringBoot_HelloWorld;
        
            import org.springframework.boot.SpringApplication;
            import org.springframework.boot.autoconfigure.SpringBootApplication;
        
            @SpringBootApplication
            public class SpringBootHelloWorldApplication {
             public static void main(String[] args) {
            SpringApplication.run(SpringBootHelloWorldApplication.class, args);
            }
        
             }
        
            Here is controller package class 
            -----------------------------------------
            package com.Encee.SpringBoot_HelloWorld.controller;
        
            import org.springframework.web.bind.annotation.RequestMapping;
            import org.springframework.web.bind.annotation.RestController;
        
            @RestController
        
            public class Controller {
                @RequestMapping("/hello.html")
               public String hello() {
                    return "Hello World";
                }
            }
        
            -----------------------------------------        
            Now Main file run as Java Application will get your output.
             SCREEN SHOT
        

        【讨论】:

        【解决方案5】:

        您的问题已解决,您可以在我的提交中查看更改。

        https://github.com/farooqrahu/ElenXHello/commits/master

        【讨论】:

        • 虽然这可能是答案,但请在答案本身中提供它,而不仅仅是提供链接。
        • 这不是一个有效的答案,最好作为评论。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-06
        • 2018-10-08
        • 2021-04-02
        • 1970-01-01
        • 1970-01-01
        • 2014-04-07
        相关资源
        最近更新 更多