へのマッピングをトリガーするかどうかはわかりませんができるようにあなたが404特定のエンドポイントを無効にする必要があると判断した場合、有効な条件が偽であるかどうかをチェックするインターセプタを作成し、そうであればそれに応じてレスポンスを設定することができます。例えば
:春ブーツで
@Component
public class ConditionalRejectionInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String requestUri = request.getRequestURI();
if (shouldReject(requestUri)) {
response.setStatus(HttpStatus.NOT_FOUND.value());
return false;
}
return super.preHandle(request, response, handler);
}
private boolean shouldReject(String requestUri) {
// presumably you have some mechanism of inferring or discovering whether
// the endpoint represented by requestUri should be allowed or disallowed
return ...;
}
}
、独自のインターセプタを登録するだけでWebMvcConfigurerAdapter
を実装する必要。例:
@Configuration
public class CustomWebMvcConfigurer extends WebMvcConfigurerAdapter {
@Autowired
private HandlerInterceptor conditionalRejectionInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// you can use .addPathPatterns(...) here to limit this interceptor to specific endpoints
// this could be used to replace any 'conditional on the value of requestUri' code in the interceptor
registry.addInterceptor(conditionalRejectionInterceptor);
}
}
'if(!enabled)'ロジックは、 'RequestMappingHandlerMapping'のカスタム実装よりもはるかに簡単です。 Togglzのようなトグル機能のフレームワークを見てください。 – nbrooks
これはいかがですか? https://stackoverflow.com/questions/44456388/conditionalonexpression-on-a-class-object-getter –
インターセプタ(URLパスベース)またはControllerAdvice(これはあなたが望むものに近いかもしれません) 。また、DispatcherServletからの突き刺しを開始することもできますが、私はそのb4を試みたことがありません。 – hummingV