question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot ignore default model on redirect after upgrading Spring Boot

See original GitHub issue

I’ve upgraded Spring Boot from 1.3.5 to 1.5.2 in this commit https://github.com/sorra/sage-system/commit/aa67c6f67a8ada51940c3dbf1c22241caaac2b78

Then when I return redirect:$someURL in a controller, it adds model attributes to the URL, then the URL is too long and fails.

In 1.3.5 there are no model attributes on redirect, but in 1.5.2 that behavior is broken.

I’ve read 1.4 and 1.5 release notes but got no information.

Even if I add explicit spring.mvc.ignore-default-model-on-redirect=true to application.properties, it still doesn’t work.

  • Note 1: I also changed the inheritance of my Application class because WebMvcAutoConfigurationAdapter has changed a lot:

    [Kotlin code]

 @SpringBootApplication
 -open class Application : WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter() {
 +@EnableWebMvc
 +class Application : WebMvcConfigurerAdapter() {
  • Note 2: My default model attributes are added via a @ControllerAdvice with @ModelAttribute

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
bclozelcommented, Apr 10, 2017

I couldn’t reproduce this issue with Spring Boot 1.4.6. Creating a new project with start.spring.io and adding the following classes shows that the feature is working as expected:

@ControllerAdvice
public class MyAdvice {

	@ModelAttribute
	public void addDefaults(Model model) {
		model.addAttribute("foo", "bar");
	}
}
@Controller
public class TestController {

	@GetMapping("/")
	public String home() {
		return "redirect:/redir";
	}

	@GetMapping("/redir")
	@ResponseBody
	public String redir() {
		return "redirected";
	}
}

The only way to reproduce the behavior you’re describing is to set the dedicated property to spring.mvc.ignore-default-model-on-redirect=false.

I believe you’re both adding @EnableWebMvc to your application configuration. Using @EnableWebMvc gives you full control over Spring MVC and disables Boot’s auto-configuration for that. This explains why Boot’s defaults and properties for MVC aren’t working anymore. There are many other ways to customize your MVC configuration with Boot.

Note that extending/overriding Spring Boot’s auto-configuration is not a good idea, for the very reason you’re experiencing: this is very brittle and a subtle change in Boot might break your application.

If I missed something here, please create a simple repro project from start.spring.io so I can investigate.

Thanks!

0reactions
kamwocommented, Apr 10, 2017

@bclozel I tried a clean project (Spring Boot 1.4.5 for comparison) and it works exactly like you described it/it should work. 👍

We use @EnableWebMvc, so it is very possible that this behavior appears in connection with this specific annotation.

Thanks for the help and the time invested into this issue!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring automatic adds model param to url - Stack Overflow
When upgrading to spring-mvc-4.0.xsd, you should replace enableMatrixVariables and ignoreDefaultModelOnRedirect with enable-matrix-variables and ignore-default- ...
Read more >
How to make spring not to add all model attributes to a redirect ...
[Solved]-How to make spring not to add all model attributes to a redirect URL?-Spring MVC ... Add ignore-default-model-on-redirect="true" to your <mvc:annotation- ...
Read more >
17. Web MVC framework - Spring
The RedirectView issues an HttpServletResponse.sendRedirect() call that returns to the client browser as an HTTP redirect. By default all model attributes are ...
Read more >
Spring Boot/MVC: Prevent Spring MVC @ModelAttribute ...
This was happening when we used @ModelAttribute variables and performed redirects. After some searchi it turned out that this is the default ......
Read more >
Top 10 Most Common Spring Framework Mistakes - Toptal
In this article we'll cover some of the more common mistakes in Spring, specifically oriented towards web applications and Spring Boot. As Spring...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found