Skip to content

WebDataBinder.getTarget() is null; Incompatible form submission retrieval after upgrading spring-framework from 5.3.x to 6.2.10 #35648

@dexchong

Description

@dexchong

It can be reproduced.

Spring 5.3.x works normally; it is recommended that 6.2.10 maintains backward compatibility: WebDataBinder.getTarget() is null

  • Purpose: Map alias values of Query parameters to actual fields via @ControllerAdvice and @InitBinder?
@ControllerAdvice
public class GlobalInitBinder {

    @InitBinder
    public void initBinder(WebDataBinder binder, HttpServletRequest request) {
        //Spring 6 fails to retrieve Params parameter objects, with binder.getTarget() = null; 
        // please help troubleshoot the issue.
        Object target = binder.getTarget();
        if (target == null) {
            return;
        }
        Map<String, String[]> parameterMap = request.getParameterMap();
        MutablePropertyValues mpv = new MutablePropertyValues(parameterMap);
        Field[] fields = target.getClass().getDeclaredFields();
}
@RestController
@RequestMapping("v1")
public class TestController {

    @PostMapping("/test")
    public void getLoadWeightRuleList(Params params) {}
}
import lombok.Data;
@Data
public class Params implements Serializable {
    @FormFieldAlias(alternateNames = {"PageNum", "PageNumber", "pageNum"})
    private Integer pageNum;
}
import org.springframework.web.bind.annotation.BindParam;

public class Params implements Serializable {
    @BindParam("PageNumber")
    private Integer pageNum;
    public Params(Integer pageNum) {
        this.pageNum = pageNum;
    }
}
  • to use HandlerMethodArgumentResolver to solve the binding of multiple aliases to fields during query submission, or to use to ServletRequestDataBinder to invoke bind(ServletRequest)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: feedback-providedFeedback has been providedstatus: waiting-for-triageAn issue we've not yet triaged or decided on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions