【问题标题】:Request Mapping not working in Spring Boot请求映射在 Spring Boot 中不起作用
【发布时间】:2018-01-18 07:35:34
【问题描述】:

我使用 Spring Boot 开发了一个简单的网页,我使用类和方法级别请求映射注释的组合,但它在以下场景下不起作用。

当我点击http://localhost:9999/products 时工作 当我点击http://localhost:9999/home/products时不工作

控制器类: 包 com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value= {"/","/home/"})
public class MainController {

@RequestMapping(value = {"products"},method=RequestMethod.GET)

public String index()
{
    return "Home.html";
}

}

【问题讨论】:

  • 您收到的错误是什么?将日志级别设置为调试并检查 URL 映射,您应该会看到一行确认您的 URL 到 index() 函数的映射。
  • 你能分享你的 application.properties 和 View Resolver 映射吗..
  • 错误我收到 Whitelabel 错误页面 此应用程序没有 /error 的显式映射,因此您将其视为后备。出现意外错误(类型=未找到,状态=404)。没有可用的消息
  • Application.properties 文件只有一个条目,即 server.port=9999

标签: spring-boot controller request-mapping


【解决方案1】:

@RequestMapping(value= {"/","home"})

【讨论】:

  • 我认为删除 / 不会有任何影响@harsh
【解决方案2】:

确保您的控制器类具有相同的包或带有注释 @SpringBootApplication 的主 Spring Boot 应用程序类的子包中,似乎只有它会扫描您的控制器。

【讨论】:

    【解决方案3】:

    下面应该可以工作..

    @Controller
    @RequestMapping(value= "/home")
    public class MainController {
    
       @RequestMapping(value = "/products",method=RequestMethod.GET)
       public String index()
       {
          return "Home.html";
       }
    
    }
    

    【讨论】:

      【解决方案4】:

      在你的类名上方添加@RestController 注解。

      【讨论】:

        猜你喜欢
        • 2019-11-25
        • 2014-05-15
        • 1970-01-01
        • 1970-01-01
        • 2014-06-12
        • 2016-12-14
        • 2022-01-12
        • 1970-01-01
        • 2018-08-12
        相关资源
        最近更新 更多