Spring-boot: Cannot ignore default model on redirect after upgrading Spring Boot

Created on 28 Mar 2017  路  6Comments  路  Source: spring-projects/spring-boot

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
invalid

Most helpful comment

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!

All 6 comments

I can confirm this behavior in the versions 1.4.4 and 1.4.5.
The setting of spring.mvc.ignore-default-model-on-redirect=true in the application.properties does not work.

I bypassed the problem in my case by a custom WebMvcConfigurerAdapter:

@Configuration
@EnableWebMvc
public class CustomWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {

    @Inject
    private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

    @PostConstruct
    public void init() {
        requestMappingHandlerAdapter.setIgnoreDefaultModelOnRedirect(true);
    }
}

I hope it will help somebody. Found the solution here

@kamwo Your workaround is really helpful!

I bypassed it by adding an interceptor to set request attributes instead of default model. Yours is simpler than mine!

@sorra you are welcome 馃槃

@bclozel Anything changed on the Spring MVC side? We have this code which should default this to true:

@Bean
@Override
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
    RequestMappingHandlerAdapter adapter = super.requestMappingHandlerAdapter();
    adapter.setIgnoreDefaultModelOnRedirect(this.mvcProperties == null ? true
            : this.mvcProperties.isIgnoreDefaultModelOnRedirect());
    return adapter;
}

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!

@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!

Was this page helpful?
0 / 5 - 0 ratings