Introduction
Lately I was creating a Spring Boot application with Spring MVC for one of my client. We noticed that variables started to appear in the URL as GET parameter. This was happening when we used @ModelAttribute
variables and performed redirects. After some searchi it turned out that this is the default behavior of Spring MVC.
Mostly it will not affect anything, but sometimes it is undesirable to show hidden parameters like settings in the URL, for example something like showHiddenFeature=false
. A user would be able to change it by setting it to true
in the URL, right? 🙂
There are multiple options to change this behavior in connection with Spring Boot.
Adjusting the settings in the properties
The easies option is to create a key in the application.properties
file
Note: The problem is, that it is ignored in some Spring Boot versions (tested it with 1.4.4 and 1.4.5)
# ===============================
# = Spring MVC
# ===============================
# Prevent @ModelAttributes to be shown in URL
spring.mvc.ignore-default-model-on-redirect=true
Overriding the setting programmatically
The other solution is to override it programmatically by extending the WebMvcConfigurerAdapter like the following:
@Configuration
@EnableWebMvc
public class MyAppWebConfig extends WebMvcConfigurerAdapter {
@Inject
private RequestMappingHandlerAdapter requestMappingHandlerAdapter;
@PostConstruct
public void init() {
requestMappingHandlerAdapter.setIgnoreDefaultModelOnRedirect(true);
}
}
If this helped you, leave a message and let me know 🙂