首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的@RequestMapping在Springboot上不起作用?

为什么我的@RequestMapping在Springboot上不起作用?
EN

Stack Overflow用户
提问于 2022-05-02 08:17:37
回答 2查看 128关注 0票数 -1

我尝试使用intellij最终版本中的springboot创建一个springboot项目,并在进行该项目时选择spring和Thymeleaf依赖项。但是,当我试图在类MainController.java下添加@RequestMapping时,@Requestmapping就不会出现。

pom.xml:

代码语言:javascript
复制
<?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 https://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.6.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo4</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo4</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <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>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>

MainController.java

代码语言:javascript
复制
package com.example.demo;

import org.springframework.stereotype.Controller;

@Controller
public class MainController {
    @RequestMap
}

主修班:

代码语言:javascript
复制
package com.example.demo;

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

@SpringBootApplication
public class Demo4Application {

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

}

如果我尝试不使用@RequestMapping运行日志,下面是这些日志

代码语言:javascript
复制
Java HotSpot(TM) 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.7)

2022-05-02 14:31:06.610  INFO 10180 --- [           main] com.example.demo.Demo4Application        : Starting Demo4Application using Java 17.0.1 on Mursalin with PID 10180 (D:\JavaFolder\demo4\target\classes started by USER in D:\JavaFolder\demo4)
2022-05-02 14:31:06.610  INFO 10180 --- [           main] com.example.demo.Demo4Application        : No active profile set, falling back to 1 default profile: "default"
2022-05-02 14:31:07.296  INFO 10180 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-05-02 14:31:07.312  INFO 10180 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-05-02 14:31:07.312  INFO 10180 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.62]
2022-05-02 14:31:07.405  INFO 10180 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-05-02 14:31:07.421  INFO 10180 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 765 ms
2022-05-02 14:31:07.640  WARN 10180 --- [           main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2022-05-02 14:31:07.702  INFO 10180 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-05-02 14:31:07.718  INFO 10180 --- [           main] com.example.demo.Demo4Application        : Started Demo4Application in 1.708 seconds (JVM running for 2.55)

以下是文件结构:

EN

回答 2

Stack Overflow用户

发布于 2022-05-02 08:42:36

@RequestMappingorg.springframework.web.bind.annotation包中。所以你需要导入它

代码语言:javascript
复制
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/Main")
public class MainController {
票数 3
EN

Stack Overflow用户

发布于 2022-05-02 09:42:45

更新到Intellij的最新版本解决了这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72084032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档