为什么我得到Post Not Allowed(405)?

时间:2014-12-04 21:22:17

标签: angularjs spring spring-mvc

我正在使用前端的AngularJs,Bootstrap和后端的Java- Spring MVC -Spring security-Hibernate。 所以我创建了一个用户 我登录了 3.我填写了profil表格,当保存时,我有405错误。

我不明白为什么我在发帖请求时会收到405错误。

这是我从AngularJS服务发送的请求:

save : function(user) {
            return $http({
                method: 'POST',
                url: 'http://localhost:8080/app-web/user/profil/'+user.id+'/create',

                data:user
            })

我有http://localhost:8080/app-web/user/profil/49/create failed with status 405 这是我的Spring Security配置文件片段:

<sec:http realm="Protected API" use-expressions="true" auto-config="false"  create-session="stateless" entry-point-ref="unauthorizedEntryPoint" authentication-manager-ref="authenticationManager">
        <sec:custom-filter ref="authenticationTokenProcessingFilter" position="FORM_LOGIN_FILTER" />
        <sec:intercept-url pattern="/login" access="permitAll" />
        <sec:intercept-url pattern="/user/**" access="permitAll" />
        <sec:intercept-url pattern="/user/profil/**" access="permitAll" />
        <sec:intercept-url pattern="/user/**" access="permitAll" />
    </sec:http>

我的控制器:

@RequestMapping(value="/user")
public class UserProfileController {

    @RequestMapping(value="/profil/{userid}/create", method= RequestMethod.POST)
    @ResponseBody
    public String createProfil(@RequestBody User user){
        // my logic
        return "OK";
    }

}

春季日志:

21:40:40.617 [http-bio-8080-exec-4] DEBUG o.s.s.w.a.i.FilterSecurityInterceptor - Authorization successful
21:40:40.617 [http-bio-8080-exec-4] DEBUG o.s.s.w.a.i.FilterSecurityInterceptor - RunAsManager did not change Authentication object
21:40:40.617 [http-bio-8080-exec-4] DEBUG o.s.security.web.FilterChainProxy - /user/profil/49/create reached end of additional filter chain; proceeding with original chain
21:40:40.617 [http-bio-8080-exec-4] INFO  c.d.j.web.filters.SimpleCORSFilter -  Request ====> 127.0.0.1 ===> java.util.Collections$2@25709a84
21:40:40.623 [http-bio-8080-exec-4] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'app' processing POST request for [/app-web/user/profil/49/create]
21:40:40.625 [http-bio-8080-exec-4] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /user/profil/49/create
21:40:40.627 [http-bio-8080-exec-4] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/user/profil/49/create]
21:40:40.627 [http-bio-8080-exec-4] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - Matching patterns for request [/user/profil/49/create] are [/**]
21:40:40.627 [http-bio-8080-exec-4] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - URI Template variables for request [/user/profil/49/create] are {}
21:40:40.628 [http-bio-8080-exec-4] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - Mapping [/user/profil/49/create] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler@663ef60] and 1 interceptor
21:40:40.629 [http-bio-8080-exec-4] DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler@663ef60]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
21:40:40.630 [http-bio-8080-exec-4] DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler@663ef60]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
21:40:40.630 [http-bio-8080-exec-4] WARN  o.s.web.servlet.PageNotFound - Request method 'POST' not supported
21:40:40.630 [http-bio-8080-exec-4] DEBUG o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'app': assuming HandlerAdapter completed request handling
21:40:40.630 [http-bio-8080-exec-4] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
21:40:40.630 [http-bio-8080-exec-4] DEBUG o.s.s.w.a.ExceptionTranslationFilter - Chain processed normally
21:40:40.631 [http-bio-8080-exec-4] DEBUG o.s.s.w.c.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed

1 个答案:

答案 0 :(得分:1)

这是一个疏忽错误,该类未被设置为Controller。所以我将UserProfilController更改为

@Controller
public class UserProfileController {

    @RequestMapping(value="/user/profil/{userid}/create", method= RequestMethod.POST)
    @ResponseBody
    public String createProfil(@RequestBody User user){
        // my logic
        return "OK";
    }

}