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

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

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

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

  1. package com.pnudev.learnspringpnudev;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class LearnApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(LearnApplication.class, args);
  8. }
  9. }

您好控制器

  1. package com.pnudev.learnspringpnudev.controller;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.PathVariable;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. @RequestMapping("/api")
  9. public class HelloController {
  10. @GetMapping("/hello/{id}")
  11. public String sayHello(@PathVariable("id") Long id, @RequestParam(value = "s", required = false) String someParam) {
  12. return String.format("Hello world! Id: %s; someParam: %s", id, someParam);
  13. }
  14. @GetMapping("/hi")
  15. public String hi(){
  16. return "hi";
  17. }
  18. }

聚合物.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>3.0.3</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.pnudev</groupId>
  12. <artifactId>learn-spring-pnu-dev</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>learn</name>
  15. <description>learn</description>
  16. <properties>
  17. <java.version>17</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-freemarker</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.projectlombok</groupId>
  30. <artifactId>lombok</artifactId>
  31. <optional>true</optional>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  38. </dependencies>
  39. <build>
  40. <plugins>
  41. <plugin>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-maven-plugin</artifactId>
  44. <configuration>
  45. <excludes>
  46. <exclude>
  47. <groupId>org.projectlombok</groupId>
  48. <artifactId>lombok</artifactId>
  49. </exclude>
  50. </excludes>
  51. </configuration>
  52. </plugin>
  53. </plugins>
  54. </build>
  55. </project>

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

798qvoo8

798qvoo81#

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

ttvkxqim

ttvkxqim2#

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

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

相关问题