弹簧启动控制器404 [英] Spring Boot Controller 404

查看:158
本文介绍了弹簧启动控制器404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的Spring启动,并试图构建一个简单的网络应用程序。我已经定义了一个控制器类包含我的映射的url,但在浏览器它给我一个白色标签页错误(404)。我不能理解为什么它不能映射。我尝试更改我的组件扫描基础包,但它仍然不会重定向到页面abc或在控制台中打印控制器。

I am fairly new in Spring Boot and trying to build a simple web app. I have defined a controller class containing my mapping for url, but on browser it is giving me a white label page error(404). I am not able to understand why it is not able to map. I have tried changing my component scan base package, but it still doesn't redirect me to page "abc" or print "in controller" in the console.

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>bms</groupId>
    <artifactId>com.wellmanage.bms</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

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

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

    <dependencies>


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

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


</project>

控制器类

package com.wellmanage.controller;   

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BookController {

    @RequestMapping("/")
    public String hello() {
        System.out.println("in controller");
        return "abc";
    }
}

主类 p>

Main class

package com.wellmanage.bms;

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

@SpringBootApplication
public class BmsApplication {

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


推荐答案

BmsApplication 类置于 com.wellmanage.bms 包下,默认情况下 @SpringBootApplication 注释使用此包作为根运行组件扫描。由于 BookController com.wellmanage.controller 中,因此默认配置会忽略此包。

The BmsApplication class is placed under the com.wellmanage.bms package and by default the @SpringBootApplication annotation runs component scan using this package as the root. Since the BookController is inside com.wellmanage.controller, the package is omitted by default configuration.

您有两个选项:


  1. 更改默认组件扫描设置, scan:



    @ComponentScan("com.wellmanage")
    @SpringBootApplication
    public class BmsApplication {

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




    >将主应用程序类移动到应用程序的根包,以便您要自动扫描的所有组件都在其包下,例如到 com.wellmanage
  1. Move the main Application class to root package of you app so that all components which you want to scan automatically are under its package, e.g. to com.wellmanage.

您没有设置任何模板引擎。从控制器返回字符串abc因此被忽略。

From the configuration you have posted, it seems you didn't set any template engine. Returning the String "abc" from your controller is hence ignored.

这篇关于弹簧启动控制器404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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