New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot ignore default model on redirect after upgrading Spring Boot #8760

Closed
sorra opened this Issue Mar 28, 2017 · 6 comments

Comments

Projects
None yet
5 participants
@sorra
Copy link

sorra commented Mar 28, 2017

I've upgraded Spring Boot from 1.3.5 to 1.5.2 in this commit sorra/sage-system@aa67c6f

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

@sorra sorra changed the title Cannot ignore model on redirect after upgrading Spring Boot Cannot ignore default model on redirect after upgrading Spring Boot Mar 28, 2017

@kamwo

This comment has been minimized.

Copy link
Contributor

kamwo commented Mar 30, 2017

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

@sorra

This comment has been minimized.

Copy link

sorra commented Mar 30, 2017

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

@bclozel bclozel self-assigned this Mar 31, 2017

@kamwo

This comment has been minimized.

Copy link
Contributor

kamwo commented Apr 3, 2017

@sorra you are welcome 😄

@philwebb

This comment has been minimized.

Copy link
Member

philwebb commented Apr 4, 2017

@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;
}
@bclozel

This comment has been minimized.

Copy link
Member

bclozel commented 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!

@bclozel bclozel closed this Apr 10, 2017

@bclozel bclozel removed this from the 1.4.6 milestone Apr 10, 2017

@kamwo

This comment has been minimized.

Copy link
Contributor

kamwo commented 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment