Spring REST:总是得到" 405方法不允许"如果在控制器

时间:2015-09-20 14:19:59

标签: spring spring-restcontroller spring-rest

我希望控制器方法中抛出的异常转换为 500内部服务器错误,但我在客户端获得 405方法不允许异常,没有重要的是控制器方法内部发生了什么异常。

注意:

请求映射没有任何问题,因为它在调试时会转到预期的方法。但是当从控制器方法抛出异常时,我得到了#34; 405方法不允许"在我的客户

注2:

我尝试捕获异常并使用适当的错误代码和消息抛出HttpServerErrorException,但没有任何改变!

编辑:

我使用spring security,这是配置:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

/*

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }
*/

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/session/**").permitAll()
                .antMatchers("/app/**").permitAll()
                .antMatchers("/lib/**").permitAll()
                .antMatchers("/templates/**").permitAll()
                .antMatchers("/dial/**").permitAll()
                .antMatchers("/names/**").permitAll()
//                    .antMatchers("/workflows/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/#/login").permitAll();

        http.csrf().disable();
        http.exceptionHandling().defaultAuthenticationEntryPointFor(
                new UnauthorizedLoginUrlAuthenticationEntryPoint("/login.html"),
                new ELRequestMatcher("hasHeader('X-Requested-With','XMLHttpRequest')"));
               /* .logout()
                    .permitAll();*/
    }


}

2 个答案:

答案 0 :(得分:0)

我可以在Spring Data REST控制器的异常处理程序中找到它: -

@ControllerAdvice(basePackageClasses = RepositoryRestExceptionHandler.class)
public class RepositoryRestExceptionHandler {

    /**
     * Send {@code 405 Method Not Allowed} and include the supported {@link org.springframework.http.HttpMethod}s in the
     * {@code Allow} header.
     *
     * @param o_O the exception to handle.
     * @return
     */
    @ExceptionHandler
    ResponseEntity<Void> handle(HttpRequestMethodNotSupportedException o_O) {

        HttpHeaders headers = new HttpHeaders();
        headers.setAllow(o_O.getSupportedHttpMethods());

        return response(HttpStatus.METHOD_NOT_ALLOWED, headers);
    }
}

你可以在你的主春季启动课上试试这个(我希望你的项目类路径以文件夹COM开头): -

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan({"com"})

答案 1 :(得分:0)

实际上问题是因为存在EmbeddedServletContainerCustomizer类型的bean 我删除了它并解决了问题。

相关问题