Mapping problem with a demo app in Spring Boot

59 views Asked by At

I have an error with my demo app in Spring Boot.
I use Spring Tool Suite 4.
The browser at the endpoint ciao shows me an error at the page http://localhost:8080/ciao

**********************************************************************************
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Jan 13 16:58:33 CET 2024
There was an unexpected error (type=Not Found, status=404).
No message available
**********************************************************************************

This is the code (HelloApplication.java):


package com.example.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;


@SpringBootApplication
@ComponentScan(basePackageClasses = HelloApplication.class)
public class HelloApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
    }
    
    @GetMapping("/ciao")
    public String salute(@RequestParam(value = "name", defaultValue="Mondo") String name) {
        return "ciao " + name;
    }
    

}

the pom.xml code is:

<?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.1.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>sommaAlert</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello</name>
    <description>hello starter</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>           
            
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</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-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-hal-explorer</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <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>
                <configuration>
                    <image>
                        <builder>paketobuildpacks/builder-jammy-base:latest</builder>
                    </image>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

The first app demo with endpoint hello worked but I then deleted the demo project and recreated it.
Seeing that there was the error of Whitelabel Error Page I then inserted these tags:

<groupId>org.apache.tomcat.embed</groupId>.
            <artifactId>tomcat-embed-jasper</artifactId>.
            <scope>provided</scope>
        </dependency> 

and

                <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-hal-explorer</artifactId>
        </dependency> 

but the error persisted.
I also tried to insert the following annotation because I read in an answer on your forum that there might be a library dependency problem:

@ComponentScan(basePackageClasses = HelloApplication.class)

but the Whitelabel Error Page persists.
I'd like to solve this error and run this basic demo application.

Thank you
Andrew

2

There are 2 answers

0
Marina On

Try it: first, remove the rest code from inside the SpringBootApplication it should have only the default code. Then create a new class with this code inside and remove value = "name". like this:

@RestController
@RequestMapping
public class demo {

   @GetMapping("/ciao")
   public String salute(@RequestParam(defaultValue="Mondo") String name) {
      return "ciao " + name;

}
1
Andrew dev On

The issue lies in the @RestController annotation. HelloApplication is a REST application, and hence, the @RestController annotation should be applied there.


@SpringBootApplication
@RestController
public class HelloApplication(){

Thank you very much for the suggestion.

Thank you very much Andrew