为什么Spring Boot示例不会像这样工作?

时间:2014-11-20 15:47:42

标签: spring annotations http-status-code-404 spring-boot

所以有一个来自Spring网站的例子:

package com.example;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Example {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

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

工作正常。备注@RestController@Controller + @ResponseBody,而@SpringBootApplication注释相当于使用@Configuration@EnableAutoConfiguration@ComponentScan

我的问题是:为什么会出现404错误访问" /"如果我在下面使用注释?

@ResponseBody
@SpringBootApplication
public class Example { ... }

2 个答案:

答案 0 :(得分:2)

单独使用@ResponseBody注释并不足以告诉Spring您的类是MVC控制器。您需要使用@RestController

@RestController
@SpringBootApplication
public class Example { ... }

@Controller@ResponseBody

@Controller
@ResponseBody
@SpringBootApplication
public class Example { ... }

答案 1 :(得分:0)

pom

insert into Customers (CustomerID, CustomerName, Address, City, PostalCode, Country)
    values
        (3, 'Antonio Moreno Taquería', 'Mataderos 2312', 'México D.F.', 05023,  'Mexico'),
        (4, 'Around the Horn', '120 Hanover Sq.', 'London', 110025, 'UK'),
        (5, 'Berglunds snabbköp', 'Berguvsvägen 8', 'Luleå', 0022,  'Sweden'),
        (6, 'Blauer See Delikatessen', 'Forsterstr. 57', 'Mannheim', 68306, 'Germany'),
        (7, 'Blondel père et fils', '24 place Kléber',  'Strasbourg', 67000, 'France'),
        (8, 'Bólido Comidas preparadas', 'C/ Araquil, 67', 'Madrid', 28023, 'Spain'),
        (9, 'Bon app''', '12, rue des Bouchers',    'Marseille', 13008, 'France'),
        (10, 'Bottom-Dollar Marketse', '23 Tsawassen Blvd.', 'Tsawassen',284, 'Canada')

主要功能

<?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">
    <artifactId>quick-log</artifactId>
    <groupId>com.quick</groupId>
    <version>1.0-SNAPSHOT</version>
    <modelVersion>4.0.0</modelVersion>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

控制器

@SpringBootApplication
public class Application {

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

容易工作〜

您可以像这样在github中找到一些快速简单的示例

https://github.com/vector4wang/spring-boot-quick