请求方法POST不受支持

时间:2015-05-03 23:14:29

标签: java angularjs spring spring-boot

GET在同一个控制器中工作正常。我已经事先研究过这个错误,不幸的是没有解决方案对我有用。我创建了我能想象的最简单的帖子,服务器仍然用

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

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

请求标题

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

}

角度文件view.js访问的位置

$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);
    }


}

我如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

Chrome错误告诉您该路线不存在。您需要向

提出请求

/api/post2

而不是

/api/test2