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

如何解决SpringBoot中的405 Method Not Allowed错误?

405 Method Not Allowed错误排查与解决

我在Postman中访问URL http://localhost:8080/registration 时,期望收到消息"Works",但实际遇到405 Method Not Allowed错误。尝试过替换@Controller为@RestController、修改部分注解均未成功,使用IntelliJ调试也未获得有效信息,现寻求解决方法。


相关代码

WebSecurityConfig类

package com.example.finalVersionBookProject.security.config;

import com.example.finalVersionBookProject.appuser.AppUserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private final AppUserService appUserService;
    private final BCryptPasswordEncoder bCryptPasswordEncoder;
    
    public WebSecurityConfig(AppUserService appUserService, BCryptPasswordEncoder bCryptPasswordEncoder) {
        this.appUserService = appUserService;
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    }
}

Registration类

package com.example.finalVersionBookProject.registration;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping(path = "/registration")
public class Registration {
    private RegistrationService registrationService;

    @PostMapping()
    public String register(@RequestBody RegistrationRequest request) {
        return registrationService.register(request);
    }
}

RegistrationService类

package com.example.finalVersionBookProject.registration;

import org.springframework.stereotype.Service;

@Service
public class RegistrationService {
    public String register(RegistrationRequest request) {
        return "works";
    }
}

解决方法

  1. 核对请求方法匹配性
    你的Registration类仅定义了@PostMapping接口,务必确认Postman中使用的是POST请求,而非GET、PUT等其他方法。若需支持GET请求,可添加@GetMapping注解的对应方法。

  2. 修正控制器注解与返回逻辑
    使用@Controller时,返回的字符串默认会被解析为视图名称,而非直接作为响应体返回。要返回纯文本"works",有两种可行方式:

    • 直接将@Controller替换为@RestController(该注解包含@Controller和@ResponseBody,自动将返回值转为响应体);
    • 保留@Controller,在register方法上添加@ResponseBody注解。
  3. 完善Spring Security配置
    你的WebSecurityConfig类未实现configure(HttpSecurity http)方法,默认会拦截所有请求并要求认证。需添加配置允许/registration路径匿名访问:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
            .authorizeRequests()
            .antMatchers("/registration")
            .permitAll()
            .anyRequest()
            .authenticated();
    }
    
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(daoAuthenticationProvider());
    }
    
    @Bean
    public DaoAuthenticationProvider daoAuthenticationProvider() {
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
        provider.setPasswordEncoder(bCryptPasswordEncoder);
        provider.setUserDetailsService(appUserService);
        return provider;
    }
    

    这段配置关闭了CSRF(Postman测试可先关闭,若后续对接表单可再开启),允许/registration路径无需认证即可访问,同时配置了DaoAuthenticationProvider完成用户认证逻辑。

  4. 检查请求体格式
    @RequestBody注解要求请求体为JSON格式,需确认Postman中请求头设置了Content-Type: application/json,且请求体符合RegistrationRequest类的结构,例如:

    {
        "username": "test",
        "password": "123456"
    }
    

    若未定义RegistrationRequest类,需先创建该实体类,包含对应字段及getter/setter方法。


内容的提问来源于stack exchange,提问作者Dave Patel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 00:45:37