You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot注释GetMapping后访问首页报405错误的原因咨询

Spring Boot根路径仅配置POST请求时访问首页报405错误的解决办法

问题原因

访问localhost首页时,浏览器默认发送GET请求。当你注释掉@GetMapping("/")后,根路径/仅配置了@PostMapping来处理POST请求,Spring MVC找不到能匹配GET请求的映射方法,就会抛出HttpRequestMethodNotSupportedException,返回405状态码(请求方法不允许)。POST方法仅在表单提交时触发,但首页访问本身就是GET请求,必须有对应的GET映射处理初始页面加载。

解决方案

方案1:恢复根路径的GET映射

保留@GetMapping("/")方法,专门处理首页GET请求,加载表单页面:

@GetMapping("/")
public String hii(Model model) {
    model.addAttribute("this", new Singers());
    return "singers";
}

方案2:合并请求方法(业务允许时)

如果希望同一个路径同时支持GET和POST,可使用@RequestMapping指定支持的请求方法:

import org.springframework.web.bind.annotation.RequestMethod;

@RequestMapping(value = "/", method = {RequestMethod.GET, RequestMethod.POST})
public String handleHome(@ModelAttribute Singers ss, Model model) {
    // 判断是否为GET请求(表单未提交时对象属性为空)
    if (ss.getArjitSingh() == null) {
        model.addAttribute("this", new Singers());
        return "singers";
    } else {
        model.addAttribute("this", ss);
        return "Songs";
    }
}

方案3:为POST请求配置独立路径

将POST请求映射到单独路径(如/submit),根路径仅保留GET请求处理页面加载:

@GetMapping("/")
public String hii(Model model) {
    model.addAttribute("this", new Singers());
    return "singers";
}

@PostMapping("/submit")
public String home(@ModelAttribute Singers ss, Model model){
    model.addAttribute("this", ss);
    return "Songs" ;
}

同时修改singers.html中的表单提交路径:

<form action="#" th:action="@{/submit}" th:object="${this}" method="post">

相关代码参考

注释GetMapping后的控制器类

package com.aman.music.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import com.aman.music.Singers;

@Controller
public class MyController { 
    @PostMapping("/")
    public String home(@ModelAttribute Singers ss,Model model){
        model.addAttribute("this",ss);
        return "Songs" ;
    }
//  @GetMapping("/")
//  public String hii(Model model) {
//      model.addAttribute("this",new Singers());
//      return "singers";
//  }
}

完整控制器类(未注释GetMapping)

package com.aman.music.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import com.aman.music.Singers;

@Controller
public class MyController {

    @PostMapping("/")
    public String home(@ModelAttribute Singers ss,Model model){
        model.addAttribute("this",ss);
        return "Songs" ;
    }
    @GetMapping("/")
    public String hii(Model model) {
        model.addAttribute("this",new Singers());
        return "singers";
    }   
}

实体类Singers.java

package com.aman.music;
public class Singers {
    private String ArjitSingh;
    private String Singer;
    private String hello;

    public String getHello() {
        return hello;
    }
    public void setHello(String hello) {
        this.hello = hello;
    }
    public String getArjitSingh() {
        return ArjitSingh;
    }
    public void setArjitSingh(String arjitSingh) {
        ArjitSingh = arjitSingh;
    }
    public String getSinger() {
        return Singer;
    }
    public void setSinger(String singer) {
        Singer = singer;
    }
    @Override
    public String toString() {
        return "Singers [ArjitSingh=" + ArjitSingh + ", Singer=" + Singer + ", hello=" + hello + "]";
    }   
}

模板页面singers.html

<!DOCTYPE html>
    <html xmlns:th="https://www.thymeleaf.org">
    <head>
    <meta charset="UTF-8" http-equiv="Content-Type">
    <title>Singer</title>
    </head>
    <body>
    <h1>ENTER SINGER NAME</h1>
    <form action="#" th:action="@{/}" th:object="${this}" method="post">
    <p>Enter Singer name<input type="text" th:field="*{ArjitSingh}"/>
    <p>Enter Singer name<input type="text" th:field="*{Singer}"/>
    <p>Enter Singer name<input type="text" th:field="*{hello}"/>
    <p><input type="submit" value="SUBMIT"/>
    </form>

    </body>
    </html>

模板页面Songs.html

<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Songs List</title>
</head>
<body>
<p>
<p><a href="https://www.youtube.com/watch?v=VAdGW7QDJiU">CHALEYA</a>
<p><a href="https://www.youtube.com/watch?v=RLzC55ai0eo">HERIYE HERIYE</a>
<p><a href="https://www.youtube.com/watch?v=HQ4Ox7mLqds">MUSKURANE KE WAJAH</a>
<p><a href="https://www.youtube.com/watch?v=_iktURk0X-A">PHIR BHI TUMKO CHAHUNGA</a>
</body>
</html>

内容的提问来源于stack exchange,提问作者Aman Asutosh Kar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 12:55:53