【问题标题】:Spring Boot and Rest Services (405 Method not Supported)Spring Boot 和 Rest 服务(不支持 405 方法)
【发布时间】:2015-07-26 11:01:42
【问题描述】:

我创建了一个简单的 Spring Boot 应用程序并创建了一个 Rest Service,当我尝试访问它时出现错误

405 : Method Not Supported

不确定是什么问题。我检查了方法注释并指定了method=RequestMethod.POST,我正在提交一个带有post方法的表单。

这是我的代码。

@SpringBootApplication
public class SsFirstApplication {

    public static void main(String[] args) {
        SpringApplication.run(SsFirstApplication.class, args);

    }
}

其他服务

@RestController
@RequestMapping("/api")
public class UserXAuthTokenController {

    @Inject
    private UserDetailsService userDetailsService;

    @RequestMapping(value = "/authenticate",
            method = RequestMethod.POST)

    public UserDetails authorize(@RequestParam String username, @RequestParam String password) {
        UserDetails details = this.userDetailsService.loadUserByUsername(username);
        return details ;
    }
}

而且我的 index.html 页面非常基础。

<html>
    <body>
        <h3>Welcome</h3>
        <form action="/api/authenticate" method="post">
            <div>
                <div> 
                    <label>User Name : </label>
                    <input type="text" name="username"/>
                </div>
                <div> 
                    <label>Password : </label>
                    <input type="password" name="password"/>
                </div>  
                <div> 
                    <input type="submit" value="Submit"/>
                </div>
            </div>
        </form>
    </body>
</html>

这是控制台日志

2015-05-14 13:38:37.525  INFO 8124 --- [nio-9090-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2015-05-14 13:38:37.525  INFO 8124 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2015-05-14 13:38:37.565  INFO 8124 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 40 ms
2015-05-14 13:38:37.590  WARN 8124 --- [nio-9090-exec-1] o.s.web.servlet.PageNotFound             : Request method 'POST' not supported

不知道我做错了什么。感谢您的回复。

【问题讨论】:

  • 您使用的是执行器吗? Actuator 为您提供了一个 /mappings 端点,可以帮助诊断问题。如果没有,请将其添加到您的 pom 中,然后在运行您的应用程序后转到您的 url/mappings,它应该显示它知道的所有映射。 org.springframework.bootspring-boot-starter-actuator

标签: rest spring-boot spring-ws


【解决方案1】:

我能够解决问题。我在主类中添加了以下注释 @EnableAutoConfiguration @ComponentScan。

现在我的主类看起来像这样。

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan({"com"})
public class SsFirstApplication {
public static void main(String[] args) {
        SpringApplication.run(SsFirstApplication.class, args);

    }
}

我认为这些是由@SpringBootApplication 自动添加的,但显然它们不是。谢谢

【讨论】:

猜你喜欢
  • 2015-12-05
  • 2016-09-23
  • 1970-01-01
  • 2021-06-09
  • 2016-07-29
  • 2014-06-10
  • 1970-01-01
  • 2014-06-14
  • 2017-07-01
相关资源
最近更新 更多