Spring Boot 我的Sping Boot RestController无法正常工作[已关闭]

b1uwtaje  于 2023-03-02  发布在  Spring
关注(0)|答案(2)|浏览(141)

**已关闭。**此问题为not reproducible or was caused by typos。当前不接受答案。

这个问题是由打字错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
昨天关门了。
Improve this question
我有一个简单的Spring应用程序,它有LearnApplication和HelloController类。HelloController不能正常工作。我希望看到“hi”这个词,但是有一个错误页面。有人能找出错误在哪里吗?
下面是我的源代码:学习应用程序

package com.pnudev.learnspringpnudev;

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);
    }

}

您好控制器

package com.pnudev.learnspringpnudev.controller;

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

@RestController
@RequestMapping("/api")
public class HelloController {

    @GetMapping("/hello/{id}")
    public String sayHello(@PathVariable("id") Long id, @RequestParam(value = "s", required = false) String someParam) {
        return String.format("Hello world! Id: %s; someParam: %s", id, someParam);
    }

    @GetMapping("/hi")
    public String hi(){
        return "hi";
    }

}

聚合物.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>3.0.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.pnudev</groupId>
    <artifactId>learn-spring-pnu-dev</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>learn</name>
    <description>learn</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

如果我运行这个项目,并打开http://localhost:8080/api/hi我有白标签错误页面在我的屏幕上。我一直在寻找解决方案几天,但没有帮助。

798qvoo8

798qvoo81#

请检查application.properties文件中的server.servlet.context-path值和端口号
如果存在上下文路径值,请将其添加到端口号之后。例如:localhost:8080/您的上下文路径值/ rest终结点

ttvkxqim

ttvkxqim2#

我用pom.xml测试了你的项目代码,一切都很好
下面是我得到的输出

你能再做一次maven clean构建并尝试吗?

相关问题