Search code examples
javaspringspring-bootspring-mvc

SpringMVC accept form data


@NoArg
data class User(
    var username: String,
    var password: String,
    // ...
)
// bean
public class User {
    private int id;
    private String firstname;
    private String lastname;
    private String username;
    private String password;
    private String email;
    // ...
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", firstname='" + firstname + '\'' +
                ", lastname='" + lastname + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

// controller
@Controller
public class UserController {
    @PostMapping("/user")
    public String getUser(User user){
        System.out.println(user);
        return "success";
    }
}
<form th:action="@{/user}" method="post">
    username:
    <label>
        <input type="text" name="userName">
    </label>
    password:
    <label>
        <input type="password" name="passWord">
    </label>
    <!-- .... -->
    <input type="submit" value="submit">
</form>

form The name field of the form is consistent with the Java properties will work fine, what if the form has a lot of content and the fields are inconsistent with the Java bean properties. As shown above. 😊

The above code will work fine. output:

User{username=a, password=a}

Solution

  • You can use @JsonAlias or @JsonProperty. The former accepts multiple deserialization fields without affecting the serialized output field, while the latter affects both deserialization and serialization fields.

    For example, let's say the input field name is userName=jack, and the Java bean is as follows: class User { @JsonAlias("userName") private String username; }

    Now, username = jack. When serialized, the output will be:

    {"username":"jack"}
    

    If you use @JsonProperty("userName"), the username will still be jack, but when serialized, the output will be:

    {"userName":"jack"}
    

    Use HandlerMethodArgumentResolver

          @Component
          public class UserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver,WebMvcConfigurer  {
    
                @Override
                public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
                    resolvers.add(this);
                }
    
        
                @Override
                public boolean supportsParameter(MethodParameter parameter) {
                    return parameter.getParameterType() == User.class;
                }
        
                @Override
                public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
                    HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
                    if(request == null){
                        return null;
                    }
                    User user = new User();
                    user.setUsername(request.getParameter("userName"));
                    return user;
                }
            }