java Sping RestController未响应请求

hrirmatl  于 2023-02-07  发布在  Java
关注(0)|答案(1)|浏览(138)

我只是试图在Spring Boot上实现一个示例REST控制器用于学习。当我通过Web浏览器发送请求时,我得到了一个白色标签错误。

我的应用程序上下文路径设置为/learn。下面是我的源文件、项目结构和pom.xml。

    • 测试控制器. 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";
    }
}
    • 聚合物. 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>
    • 学习应用程序. 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);
    }
    

}
    • 应用程序.属性**
server.port=8081
server.servlet.context-path=/learn
wqsoz72f

wqsoz72f1#

因为你已经在你的配置文件中设置servlet上下文路径为'/learn',你不需要再在你的控制器的RequestMapping中添加'/learn'.只是删除它并让我们知道.

相关问题