Title: @ModelAttribute populate form field from request param instead of backing form
Tags: java;spring;spring-boot;spring-mvc;modelattribute
Question: My problem is:
@ModelAttribute populate form field from request param instead of form DTO if request param and form field has same name.
Example: I have form with input filed called name:
<input type="text" name="name" /&gt;
Given form with value name=John,
If I submit form (web method POST) using url:
http://localhost:8080/user/?name=Michael
I will have query param and form field that has same name.
What I expect is: name field should be populated from Form Field, not query params.
MyForm.java
public class MyForm {
private String name;
private Boolean isMale;
private Byte status;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
public Boolean getIsMale() {
return isMale;
public void setIsMale(Boolean isMale) {
this.isMale = isMale;
public Byte getStatus() {
return status;
public void setStatus(Byte status) {
this.status = status;
MyController.java
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(value = "/", method = RequestMethod.POST)
public String index(
Model model,
@ModelAttribute("form") MyForm form,
BindingResult bindingResult) {
String name = form.getName(); //this contains value from form: Michael
Boolean isMale = form.getIsMale(); //this contains value from query parameter: true
Byte status= form.getStatus(); //this contains value from form: 1
return "views/index";
When I submit the form with values:
name = Michael
isMale = false
status = 1
using url with query params:
http://localhost:8080/user/?isMale=true
then isMale will contains value true, that populate from query param.
What I expects is, isMale should contains false that populate from Form Field.
How to solve this problem...?


Here is another answer: You should use @RequestBody annotation instead of @ModelAttribute if you want just the request body (which contains your form data) to populate your Java object
(as a side note, its bad design to pass the same parameter name in both your query string as well as in your form data. Do refactor, if possible)
Comment for this answer: Actually, index() method will have @RequestMapping with method POST and GET:
@RequestMapping(value = "/", method = { RequestMethod.POST, RequestMethod.GET }). So, if user visit `http://localhost:8080/user/?name=Michael` he will have form with name initialized (Michael), then he may change name value to Jonh and submit the form.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:04/21/19', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<java><spring><spring-boot><spring-mvc><modelattribute>', 'Title': '@ModelAttribute populate form field from request param instead of backing form', 'ViewCount': 482, 'id': 55785018, 'user_AboutMe': None, 'user_AccountId': 12960782.0, 'user_DisplayName': 'Mohd Erwin', 'user_Reputation': 33.0, 'user_Views': 85.0, 'user_id': 9370103.0}}