ChadBorzhky
ChadBorzhky

Reputation: 13

How to make spring not to add all model attributes to a redirect URL?

When using old-styled (extends *Controller) controllers before we often used return new ModelAndView(new RedirectView("/url")) to return redirects from a Spring controller.

After upgrading to Spring 4.0 (@Controller) we've started to use return "redirect:/url" to do the same from @RequestMapping-annotated methods. However we've noticed that Spring adds all model attributes to a resulting redirect URL. This is highly undesirable behavior for us.

What makes matters worse: since those are added to a "Location" HTTP header (standard for HTTP 302) this makes Tomcat to throw an the following exception:

Aug 11, 2014 2:42:31 PM org.apache.coyote.ajp.AjpProcessor process
SEVERE: Error processing request
java.lang.ArrayIndexOutOfBoundsException: 8192
    at org.apache.coyote.ajp.AjpMessage.appendByte(AjpMessage.java:146)

One solution that comes to mind is clearing the model before every redirect, however this is rather cumbersome.

Is there a way to configure Spring not to add all model attributes to a redirect url?

Upvotes: 1

Views: 3072

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 148890

In a new application, or if your application does not depend on the usage of model attributed on redirect, you should add ignore-default-model-on-redirect="true" to the <mvc:annotation-driven> tag as Aramir said.

If it concerns only few new methods where other need the legacy mode, you can add a RedirectAttribute parameter to your method. As soon as it exists, Spring uses it instead of the model to determine which attributes should be used in redirected URL.

Upvotes: 1

Aramir
Aramir

Reputation: 327

Add ignore-default-model-on-redirect="true" to your <mvc:annotation-driven> tag.

Upvotes: 4

Related Questions