blahblah
blahblah

Reputation: 1251

Request method POST not supported

GET works fine in the same controller. I have researched the error beforehand and no solution worked for me, unfortunately. I have created the simplest post i could imagine, and server still responds with

2015-05-04 01:05:56.669  WARN 6496 --- [-nio-420-exec-8] o.s.web.servlet.PageNotFound             : Request method 'POST' not supported

Response Headers in Chrome:

HTTP/1.1 405 Method Not Allowed
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Allow: HEAD, GET
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 03 May 2015 23:11:57 GMT

Request Headers

POST /api/test2 HTTP/1.1
Host: localhost:420
Connection: keep-alive
Content-Length: 0
Accept: application/json, text/plain, */*
Origin: http://localhost:420
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36 OPR/29.0.1795.47
Referer: http://localhost:420/
Accept-Encoding: gzip, deflate, lzma
Accept-Language: en-US,en;q=0.8
Cookie: JSESSIONID=06176AA355A225D9532206FCDDDEF711

ViewController

@RestController
@RequestMapping(value = "/api")
public class ViewController {

    @RequestMapping(value = "/post2", method = RequestMethod.POST)
    public String itWorked2() {
        System.out.println("hell yeah it worked");
        return "No";
    }

}

angular file view.js where it's accessed

$scope.teste2 = function() {
        $http.post(urlbase + 'api/test2').success(function(data) {
            console.log('yeah');
        }).error(function(data,status,headers,config) {
            console.log('no');
        });
};

WorkingusersApplication

@SpringBootApplication
public class WorkingusersApplication implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {

        }

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

SecurityConfiguration

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfiguartion extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private SecurityProperties security;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.
                authorizeRequests().antMatchers("/home","/","/css/**","/js/**","/api/**","/post/**").permitAll().anyRequest().
                fullyAuthenticated().and().formLogin().loginPage("/login").failureUrl("/login?error").permitAll().and().logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout")).and()
                .exceptionHandling().accessDeniedPage("/access?error").and().csrf().disable();
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(this.dataSource);
    }


}

How do i solve this issue?

Upvotes: 0

Views: 4521

Answers (1)

Martin Konecny
Martin Konecny

Reputation: 59631

The Chrome error is telling you that the route doesn't exist. You need to make a request to

/api/post2

instead of

/api/test2

Upvotes: 5

Related Questions