关于Spring mvc中modelattribute无法制定别名的解决方案

原文链接:http://rowkey.me/blog/2015/01/21/springm-mvc-model-attribute-alias/

 

最近由于项目需要,发现spring mvc在绑定参数时有这么一个缺陷。

  

Url: http://localhost:8080/api/test?user_name=testUser  

Controller:   

@Controller
@RequestMapping("/api")
public class ApiController extends BaseController {

    @RequestMapping(value = "/test", headers = "Accept=application/json")
    public void authUser(ModelMap modelMap, Account acc) {
        ResultPack.packOk(modelMap);
    }
}

public class Account{
    private static final long serialVersionUID = 750752375611621980L;

    private long id;
    private String userName;
    private String password;
    private AccountType type = AccountType.ADMIN;
    private long timeTag;
    private int status = 1;
    ...
    ...
}

 

user_name无法映射到acc的userName上。如果使用json的方式,可以使用JsonProperty注解来解决。否则,spring貌似没提供解决方案。  

于是追踪了一下spring mvc的源代码,发现可以通过重写ServletModelAttributeMethodProcessor来支持这个功能。  

github:  https://github.com/superhj1987/spring-mvc-model-attribute-alias

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值