banner
RustyNail

RustyNail

coder. 【blog】https://rustynail.me 【nostr】wss://ts.relays.world/ wss://relays.world/nostr

In the case of the same request parameters

In the processing function of the controller, it is possible to encounter a situation where the parameters of the request have the same member:

@RequestMapping(value = "path",method = RequestMethod.POST)
public Map<String,Object> addPasswordInformation(A a,B b){
    // do something here
}

If both A and B have the same member c, the corresponding values cannot be automatically bound.

Solution#

Use the InitBinder annotation.

Define binder methods#

@InitBinder("a")
public void initUserBinder(WebDataBinder webDataBinder){
    webDataBinder.setFieldDefaultPrefix("classA.");
}

@InitBinder("b")
public void initUserBinder(WebDataBinder webDataBinder){
    webDataBinder.setFieldDefaultPrefix("classB.");
}

Add annotations to the parameters of the controller method#

@RequestMapping(value = "path",method = RequestMethod.POST)
public Map<String,Object> addPasswordInformation(@ModelAttribute("a") A a, 
                                                @ModelAttribute("b") B b){
    // do something here
}

The parameter of the @ModelAttribute("..") annotation should be the same as the parameter of the @InitBinder("..") annotation.

Modify the request data#

data = {
    'classA.c':'asdasdasd',
    ,
    'classB.c':'asdasdasd',
}

The above is Python code, but it is the same. Just change the key to include the prefix.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.