0

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.

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
Sateesh K
  • 1,071
  • 3
  • 19
  • 45
  • Are you using Actuator? There is a /mappings endpoint that Actuator gives you that could help diagnose the issue. If not, add it to your pom, then after running your app go to your url/mappings and it should show all the mappings it knows about. org.springframework.boot spring-boot-starter-actuator – Kevin M May 14 '15 at 17:54

1 Answers1

1

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

Sateesh K
  • 1,071
  • 3
  • 19
  • 45
  • They are added automatically, but by default `@ComponentScan` only looks for classes in the same package as your `Application` class. See cipley's answer here http://stackoverflow.com/questions/31318107/spring-boot-cannot-access-rest-controller-on-localhost-404 – gary69 May 07 '16 at 21:32