Home > Software engineering >  Parameterless @GetMapping doesn't work in Spring
Parameterless @GetMapping doesn't work in Spring

Time:02-03

In a tutorial I'm using this code with a parameterless @GetMapping works but not for me. Is it even supposed to work? Maybe tutorial is too old for a newer spring? Maybe there's another way to map default page these days? Controller:

package com.example.bubble.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class WelcomeController {
    
    @GetMapping("/welcome")
    public String welcome(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "welcome";
    }

    @GetMapping
    public String main(Model model){
        model.addAttribute("some", "hello, bro");
        return "main";
    }
}

So I want main to be the default page. main:

<html>

<body>
    <div>{{some}}</div>
</body>
</html>

pom file also:

<?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>3.0.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>bubble</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>bubble</name>
    <description>Le Demo project for Spring Boot</description>
    <properties>
        <java.version>19</java.version>
    </properties>
    <dependencies>  

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mustache</artifactId>
            <optional>true</optional>
        </dependency>           
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</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>

Application class is in a package com.example.bubble

All I'm getting on localhost:8080 is a Whitelabel error page and "There was an unexpected error (type=Not Found, status=404). No message available". If I try main with explicit mapping it perefectly works.

CodePudding user response:

I recommend using @GetMapping("/"), this will do the same in this case as parameterless value.

  • Related