【问题标题】:404 fail while trying to run spring boot app from start.spring.io尝试从 start.spring.io 运行 Spring Boot 应用程序时出现 404 失败
【发布时间】:2016-12-20 20:18:14
【问题描述】:

当我在 Spring Boot 中启动我的迷你项目(主要是我从 start.spring.io 生成)时,从命令行通过命令 mvn spring-boot:run 并步行到 http://localhost:8080/test/{michal} 我看到 404 失败。

除此之外,我无法使用 Response.create 在控制器类中创建返回语句 -> 它显示:

未为类型 Response 定义方法 create(String)

我认为这是我的应用程序不想正常运行的主要问题。

这是我的控制器类

@RestController
public class TestController {

    @Autowired
    private TestService testService;

    @RequestMapping(value = "/test/{name}", method = RequestMethod.GET)
    public Response<String> /*@ResponseBody String*/ getWelcomeText(@PathVariable("name") String name) {
        return Response.create(testService.loadWelcomeText(name));
    }

}

这是我的服务类

@Service
public class TestService {
   public String loadWelcomeText(String name) {
       return "Hello " + name;
   }
}

我有@ComponentScan 注释

【问题讨论】:

  • 最好修改您的原始帖子,而不是添加带有详细信息的答案。你能删除你的“答案”并更新帖子吗?此外,您使用的 URL 有点奇怪 http://localhost:8080/test/{michal}。为什么它有花括号?你真的需要它们吗?

标签: spring-mvc spring-boot http-status-code-404


【解决方案1】:

我认为问题在于您使用Response 而不是ResponseEntity。不知道你在哪里得到了使用Response 的文档,但官方文档在这里描述了它:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-httpentity

使用ReponseEntity的构造函数是:http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html

【讨论】:

    【解决方案2】:

    更新

    查看包布局,您需要在主类上添加@ComponentScan("com.bielawski.michal") 注释,以告诉spring-boot 在哪里定位控制器和服务(或将主类移动到com.bielawski.michal 包)。

    【讨论】:

    • 我在上面用屏幕回答了你在我的项目中的样子
    • 我已经改变了你所说的,但 Response.create 仍然存在同样的问题
    【解决方案3】:

    是这样的:我有@ComponentScan注解

    How it looks in my project

    【讨论】:

      猜你喜欢
      • 2018-03-30
      • 2021-05-08
      • 1970-01-01
      • 2020-10-26
      • 2021-10-08
      • 1970-01-01
      • 2017-06-30
      • 1970-01-01
      相关资源
      最近更新 更多