【问题标题】:Spring @GetMapping in @Controller not found找不到@Controller中的Spring @GetMapping
【发布时间】:2020-06-09 20:14:56
【问题描述】:

我正在使用 Spring 框架。我已经尝试过使用 Rest 和 View。我在路由应用程序时遇到问题。

如果我在控制器中使用@RestController,它运行良好。但在这种情况下,我想查看用户,所以我使用@Controller。但是当我使用@Controller 时,它总是返回一个错误“未找到”。

这是我的代码:

@RestController
public class LombaController {

    @GetMapping("/get")
    public String get(Model model) {
        model.addAttribute("message", "Hello World");
        return "index";
    }
}

当我向http://localhost:8080/get 请求时,该代码有效。但它返回一个字符串“索引”而不是我的视图模板。我得到了使用@Controller 而不是@RestController 来呈现视图的解决方案。但它总是返回“未找到”错误。这是我使用@Controller时的代码:

@Controller
public class LombaController {

    @GetMapping("/get")
    public String get(Model model) {
        model.addAttribute("message", "Hello World");
        return "index";
    }
}

这是我的 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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.crudcoba</groupId>
    <artifactId>spring-crud-coba</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-crud-coba</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>

如何解决这个问题?

【问题讨论】:

  • 您需要指定确切的错误,而不是仅仅告诉我们这是“未找到”;您可能会在服务器控制台上收到有用的错误报告。很可能,您在预期位置没有名为 index.html 的模板,而且您也没有向我们展示。
  • 您好,谢谢您的回答。我在视图模板位置遇到了问题

标签: java spring spring-boot


【解决方案1】:

你可以使用

@RequestMapping(value="/get",method=RequestMethod.GET)

而不是

@GetMapping("/get")

【讨论】:

  • 感谢您的回答,主要问题出在我的资源位置
  • 它有什么帮助?
  • 我认为问题出在我的模板目录中,而不是在 GetMapping 或 RequestMapping 注释中
【解决方案2】:

@RestController@RequestBody 注释添加到您的方法中,这确实会返回String“索引”。

如果您想使用您的模板文件,请将其放入 /src/main/resources/templates/index.html 并将您的 @RestController 更改为 @Controller,这样就可以了。

【讨论】:

  • 您好,谢谢您的回答。问题是资源文件夹。我的资源文件夹在 /src/main/ 下,这是我从官方页面下载时的 spring 默认配置,在我的情况下不起作用。所以我把它移到 /src/main/java 文件夹中,它工作顺利..
  • @AdiSparta 这不是正确的位置,不应该解决问题。模板应位于src/main/resources/templates/{index.html}
  • @chrylis 那是一个“捷径”,我将编辑我的答案以包括src/main 部分以避免任何混淆
  • @chrylis 我不知道这里发生了什么,但现在当我使用来自 src/main/resources/templates/{index.html} 的模板运行时它可以工作。可能是我认为的缓存...
  • @AdiSparta 有两个步骤可能会导致麻烦。确保禁用 Thymeleaf 缓存(参见application.properties;您的 IDE 可能会为您自动完成条目)并确保您的 IDE 将源 HTML 中的更改复制到运行时类路径(Eclipse 会自动执行此操作,IntelliJ 可能需要一个项目刷新)。
猜你喜欢
  • 1970-01-01
  • 2017-11-05
  • 2022-11-25
  • 2022-12-25
  • 1970-01-01
  • 2022-11-13
  • 1970-01-01
  • 1970-01-01
  • 2019-12-06
相关资源
最近更新 更多