首页 > 解决方案 > HTTP 状态 404 - 在 Spring Boot 中调用 url 时未找到

问题描述

浏览器中的服务响应

在简单创建的 Spring Boot 应用程序的浏览器上调用 URL 时,我得到 HTTP 状态 404。下面是代码。我的代码中没有错误或问题。我只是从互联网上复制我的代码。

主要的 spring boot 应用程序类


   package com.springboot.app;
   
   import org.springframework.boot.SpringApplication;
   import org.springframework.boot.autoconfigure.SpringBootApplication;
   import org.springframework.context.annotation.ComponentScan;
   
   @SpringBootApplication
   public class SampleBootAppApplication {
   
       public static void main(String[] args) {
           SpringApplication.run(SampleBootAppApplication.class, args);
       }
   
   }

下面是控制器类

控制器类


    package com.springboot.app.controller;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class AppController {
    
        
        @GetMapping("/test")
        @ResponseBody
        public String getName()
        {
            return "adil abdullah";
        }
    }

项目结构:

spring工具套件中的项目结构

标签: springspring-boothttpspring-mvchttp-status-code-404

解决方案


访问 URL 错误:http://localhost:8080/test 它应该有上下文路径,它将在 application.properties 文件中,如 server.servlet.context.path = /samplebootapp 访问 URL:http://localhost: 8080/samplebootapp/测试


推荐阅读