Home > Blockchain >  Why isn't my @RequestMapping working on Springboot?
Why isn't my @RequestMapping working on Springboot?

Time:05-02

I have tried to create a springboot project using spring initilizr in intellij ultimate edition and selected Spring web and Thymeleaf dependencies while making that project. But when I try to add @RequestMapping under the class MainController.java , the @Requestmapping doesn't show up.

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 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

package com.example.demo;

import org.springframework.stereotype.Controller;

@Controller
public class MainController {
    @RequestMap
}

Main class :

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);
    }

}

Here is the logs if I try to run without @RequestMapping

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)

Here are the file structures :

RequestMapping doesn't show!

CodePudding user response:

@RequestMapping is in org.springframework.web.bind.annotation package. So you need to import it

import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/Main")
public class MainController {

CodePudding user response:

Updating to latest version of Intellij solved this problem

  • Related