Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is possible, but you would have to do it yourself (once).</p> <p>The interface for this is <a href="http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/web/method/support/HandlerMethodArgumentResolver.html" rel="nofollow noreferrer"><code>HandlerMethodArgumentResolver</code></a>. The way I see it is you would create an annotation, like <code>@FromUrlAndHeaders</code> and use that to annotate the parameter in the method:</p> <pre><code>@RequestMapping(value = "/someRequest/path") public String doBusiness(@FromUrlAndHeaders CustomObject customObject) { // do business with customObject } </code></pre> <p>Then the fun part is creating your own <code>HandlerMethodArgumentResolver</code>.</p> <pre><code>public class FromUrlAndHeadersHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver { public boolean supportsParameter(MethodParameter parameter) { return parameter.hasParameterAnnotation(FromUrlAndHeaders.class); } @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { // use the various objects here // request to get parameters and headers // mavContainer for model attributes (if you need) // parameter for class type and annotation attributes // etc. // note that the parameter class type matters, are your creating a CustomObject, a String, a DifferentClassObject, etc... } } </code></pre> <p>You can then register this <code>HandlerMethodArgumentResolver</code> and let it do work.</p> <hr> <p>The <code>DispatcherServlet</code> stack uses a list of <code>HandlerMethodArgumentResolver</code> implementation instances to decide what argument to pass to your method. There's one for <code>@ModelAttribute</code>, for <code>@PathVariable</code>, for <code>@RequestParam</code>, for <code>@RequestBody</code>, for <code>ModelMap</code>, for <code>HttpServletRequest</code>, for <code>HttpServletResponse</code>, basically for each parameter type supported by default. You can see all of them in the javadoc.</p> <p>Related:</p> <ul> <li><a href="https://stackoverflow.com/questions/18690736/spring-mvc-controller-with-multiple-requestbody/18690904#18690904">Spring MVC controller with multiple @RequestBody</a></li> <li><a href="https://stackoverflow.com/questions/18242454/controller-handler-method-supported-return-types/18242640#18242640">Controller handler method supported return types</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload