typeX
とをバインドする場合はtypeY
、必ず2つのハンドラーが必要です。しかし、なぜ私たちは:param
のオプションを使用しないのですか?@RequestMapping
@RequestMapping(method = RequestMethod.POST,
value = "/url", params = "session_type=typeX")
public String handleTypeX(@RequestBody @ModelAttribute TypeX typeX){
//TODO implement
}
@RequestMapping(method = RequestMethod.POST,
value = "/url", params = "session_type=typeY")
public String handleTypeY(@RequestBody @ModelAttribute TypeY typeY){
//TODO implement
}
いくつかの準備が必要な場合(パラメーターを正規化するか、モデルバインディングを手動で実行する)、上記のアプローチをと組み合わせることができますが、ハンドラーのパラメーターとともに正確なULRのルールが必要@InitBinder
であることに注意してください。@InitBinder
@ModelAttribute
編集:Spring MVCでは、正確なURLに2つのハンドラーを使用することはできません。つまり、method / URL / params/consumesタイプが同じ場合です。
したがって、必要なパラメーターを確認してから手動で対応するクラスに変換する統合ハンドラーを使用することをお勧めします。必要なクラスを見つけるには、ストラテジーパターンを使用する方が良いと思います。
//class resolver according "session_type" parameter
//note, that you can use Spring autowiring capabilities
private final Map<String, Class> TYPES_CONTEXT = new HashMap<String, Class>(){
{
this.put("x", TypeX.class);
this.put("y", TypeY.class);
//TODO probably other classes
}
}
@RequestMapping(method = RequestMethod.POST,
value = "/url")
public @ResponseBody String handleAnyType(@RequestBody Map<String, String> body){
String sessionType = body.get("session_type");
//TODO handle case if sessionType is NULL
Class convertedClass = TYPES_CONTEXT.get(sessionType);
//TODO handle case if class is not found
Object actualObject = objectMapper.convertValue(body, convertedClass);
//now we use reflection for actual handlers, but you may refactor this in the way you want, f.e. again with Strategy pattern
//note that current approach there should be contract for methods names
Method actualHandler = this.getClass().getMethod("handle" + actualObject.getClass().getSimpleName());
return (String)actualHandler.invoke(this, actualObject);
}
public String handleTypeX(TypeX typeX){
//TODO implement
}
public String handleTypeY(TypeY typeY){
//TODO implement
}
//TODO probably other methods
このアプローチは検証を処理せず、いくつかのことが省略されましたが、これは役立つかもしれないと思います。