Home > Mobile >  Sping RestController not responding to request
Sping RestController not responding to request

Time:02-04

I am just trying to implement a sample rest controller on spring boot for learning. When i am sending the request through web browser i am getting a white label error.

enter image description here

My applications context path is set to /learn. Below are my source files, project structure and pom.xml.

enter image description here

TestController.java

package com.practice.learn.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/learn")
public class TestController {

    TestController(){
        System.out.println("TestController initialized");
    }
    @GetMapping("/test")
    public String testMethod() {
        System.out.println("Inside test method");
        return "This is sample text";
    }
}

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.7.8</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.practice</groupId>
    <artifactId>learn</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>learn</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</artifactId>
        </dependency>

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

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

LearnApplication.java

package com.practice.learn;

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

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

}

application.properties

server.port=8081
server.servlet.context-path=/learn

CodePudding user response:

since you already set the servlet context path as '/learn' in your configuration file, you do not need to add '/learn' again in the RequestMapping in your controller. just remove it and let us know.

  • Related