ヴェルナー:
EDIT :回答に基づいて、私はこの書かれている HandlerInterceptorを :
REST要求が明示的に呼び出さREST方式で宣言されていないパラメータが含まれているかどうかを確認する方法は春(ブート)ではありますか?
で必要なフラグ我々は、要求内の特定のパラメータを含めるようにクライアントを強制することができます。私は明示的には、コントローラのメソッドの宣言で言及されていないパラメータを送信するためにクライアントを禁止する同様の方法を探しています:
@RequestMapping("/hello")
public String hello(@RequestParam(value = "name") String name) {
//throw an exception if a REST client calls this method and
// sends a parameter with a name other than "name"
//otherwise run this method's logic
}
例えば、通話のための
curl "localhost:8080/hello?name=world&city=London"
生じるはずである4xxの答え。
1つのオプションは、明示的に予想外のパラメータをチェックするために、次のようになります。
@RequestMapping("/hello")
public String hello(@RequestParam Map<String,String> allRequestParams) {
//throw an exception if allRequestParams contains a key that we cannot process here
//otherwise run this method's logic
}
しかし、それは同じ便利保ちながら、同じ結果を達成することも可能である@RequestParam
最初の例のように使用法を?
EDIT:申し訳ありませんが、私はへの接続が表示されないこの質問を。他の質問は、実行時に注釈処理についてです。私の質問は、SpringのRESTエンジンの動作についてです。私何か不足していますか?
EDIT :回答に基づいて、私はこの書かれている HandlerInterceptorを :
@Component
public class TooManyParamatersHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
if (!(handler instanceof HandlerMethod)) {
return true;
}
HandlerMethod m = (HandlerMethod) handler;
if (m.getMethod().getName().equals("error")) {
return true;
}
List<String> allowedParameters = Stream.of(m.getMethodParameters())
.flatMap(p -> Stream.of(p.getParameterAnnotation(RequestParam.class)))
.filter(Objects::nonNull)
.map(RequestParam::name).collect(Collectors.toList());
ArrayList<String> actualParameters = Collections.list(request.getParameterNames());
actualParameters.removeAll(allowedParameters);
if (!actualParameters.isEmpty()) {
throw new org.springframework.web.bind.ServletRequestBindingException(
"unexpected parameter: " + actualParameters);
}
return true;
}
}
デッドプール :
この場合、あなたは必要HandlerInterceptor
かHandlerInterceptorAdapter
、上書きするpreHandle
方法を
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
//request param validation validation
return true; //or throw exception
}
ServletRequest.getParameterMap()は、リクエストパラメータのキー値のマップを返します。