title: @RequestBody vs HandlerMethodArgumentResolver in Spring
body: <p>I have two solutions for the same problem but I'm not sure which solution is better to use in the long run.</p>
  
  <h1>solution 1:</h1>
  
  <p>The controller is:</p>
  
  <pre><code>@RequestMapping(value = "/skill/leatherworking/curing/start", method = RequestMethod.POST)
  public Response startCuring(final UserEntity userEntity, @RequestBody @Valid final CuringCreateRequest curingCreateRequest) {
      final CuringResult result = curingService.cure(userEntity, recipeDefinitionCache.getDefinition(curingCreateRequest.getRecipeId()));
      ...
  }
  </code></pre>
  
  <p>And the domain object is:</p>
  
  <pre><code>public class CuringCreateRequest {
  
      @Min(1)
      private int recipeId;
  
      ...
  }
  </code></pre>
  
  <h1>solution 2:</h1>
  
  <p>The controller is:</p>
  
  <pre><code>@RequestMapping(value = "/skill/leatherworking/curing/start", method = RequestMethod.POST)
  public Response startCuring(final UserEntity userEntity, @Valid final CuringCreateRequest curingCreateRequest) {
      final CuringResult result = curingService.cure(userEntity, curingCreateRequest.getRecipe());
      ...
  }
  </code></pre>
  
  <p>Here we also have a HandlerMethodArgumentResolver:</p>
  
  <pre><code>public class RequestContextHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
  
      /**
       * {@inheritDoc}
       */
      @Override
      public boolean supportsParameter(final MethodParameter parameter) {
          return parameter.getParameterType().equals(CuringCreateRequest.class);
      }
  
      /**
       * {@inheritDoc}
       */
      @Override
      public Object resolveArgument(final MethodParameter parameter, final ModelAndViewContainer mavContainer, final NativeWebRequest webRequest,
          final WebDataBinderFactory binderFactory) throws Exception {
          ... //Manually resolve the argument via JsonMapper and RecipeDefinitionCache. This way we can create an immutable class and that's a bonus too.
      }
  }
  </code></pre>
  
  <p>And the domain object is:</p>
  
  <pre><code>public class CuringCreateRequest {
  
      @NotNull
      private RecipeDefinition recipe;
  
      ...
  }
  </code></pre>
  
  <p>Which solution is better to use in the long term? Especially in a medium or big project? I prefer the <strong>solution 2</strong> because its cleaner and the controller have less responsability but not exactly sure that it's adventages are good enough for the hassle of creating another class for every different parameter. Especially where a @RequestBody can solve the problem under 2 seconds.</p>