Sateesh K
Sateesh K

Reputation: 1091

Spring Boot and Rest Services (405 Method not Supported)

I created a simple Spring Boot app and crated a Rest Service and when i tried to access it I'm getting error

405 : Method Not Supported

Not sure what's the issue. I checked the method annotations and I specified method=RequestMethod.POST and I am submitting a form with post method.

Here is my Code.

@SpringBootApplication
public class SsFirstApplication {

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

    }
}

And the Rest Service

@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 ;
    }
}

And my index.html page is pretty basic.

<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>

And here is the console log

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

Not sure what i am doing wrong. Appreciate your response.

Upvotes: 0

Views: 6800

Answers (1)

Sateesh K
Sateesh K

Reputation: 1091

I am able to resolve the Issue. I added following annotations to the main class @EnableAutoConfiguration @ComponentScan.

Now my main class looks like this.

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

    }
}

I thought these were added automatically by the @SpringBootApplication but apparently they are not. Thanks

Upvotes: 1

Related Questions