Spring 春假:总是得到“休息”;405“不允许使用方法”;如果控制器内部发生异常

Spring 春假:总是得到“休息”;405“不允许使用方法”;如果控制器内部发生异常,spring,spring-restcontroller,spring-rest,Spring,Spring Restcontroller,Spring Rest,我希望控制器方法中抛出的异常会转换为500内部服务器错误,但无论控制器方法中发生什么异常,我都会在客户端得到405方法不允许异常 注: 请求映射并没有问题,因为当我调试它时,它会转到预期的方法。但当从控制器方法抛出异常时,我在客户端中得到“405 method not allowed” 注2: 我尝试捕获异常并抛出带有适当错误代码和消息的HttpServerErrorException,但没有任何改变 编辑: 我确实使用spring安全性,以下是配置: @Configuration @Enabl

我希望控制器方法中抛出的异常会转换为500内部服务器错误,但无论控制器方法中发生什么异常,我都会在客户端得到405方法不允许异常

注: 请求映射并没有问题,因为当我调试它时,它会转到预期的方法。但当从控制器方法抛出异常时,我在客户端中得到“405 method not allowed”

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

编辑: 我确实使用spring安全性,以下是配置:

@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();*/
    }


}

我可以在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);
    }
}

实际上,问题是因为存在类型为
EmbeddedServletContainerCustomizer


我删除了它并解决了问题。

您可以将您的方法与使用的PUT请求URL一起分享吗!!!你也在使用spring security吗??如果是,那么您使用的安全配置也很有用。有一个异常通知将异常映射到405?你能提供一个复制这种行为的简单例子吗?@Avis我确实使用spring security。我在帖子中添加了我的配置。我确信这真的不是URL映射的问题。然而,我怀疑有某种异常过滤正在进行,我不知道它是从哪里来的。也许有一些关于春季安全的东西。
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan({"com"})