简体   繁体   中英

Spring @GetMapping in @Controller not found

I am using the Spring framework. I have tried a little bit using Rest and using View. I have a problem in routing the application.

If I use @RestController in my controller, it works well. But in this case I want to user view so I use @Controller . But when I use @Controller it always returns an error "not found".

This is my code:

@RestController
public class LombaController {

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

That code works when I request to http://localhost:8080/get . But it returns a string "index" instead of my view template. I get a solution to using @Controller instead of @RestController to render the view. But it always returns an error "not found". This is my code when I use @Controller :

@Controller
public class LombaController {

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

And this is my 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>

How to solve this issue?

@RestController adds @RequestBody annotation to your methods, which will indeed return a String "index".

If you want to use your template file, put it in /src/main/resources/templates/index.html and change your @RestController to @Controller , that should do the trick.

You can use

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

instead of

@GetMapping("/get")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM