Java 关闭Spring MVC数据绑定器

Java 关闭Spring MVC数据绑定器,java,spring,spring-mvc,Java,Spring,Spring Mvc,stackoverflow上的第一篇帖子。。。我开始使用SpringMVC,我试图找出将实体尽可能无状态地链接到web视图的最佳方法 我发现的一种方法是在一个方法上使用@ModelAttribute,该方法在参数中接收(来自请求)实体ID,该实体ID从服务/持久性层找到它,并返回它,以便将其插入当前请求的模型中 此外,Spring MVC绑定与my entity字段匹配的任何传入参数,并自动(通过WebDataBinder)更新其值 我的问题是关于最后的行为。我发现当客户端发布了一些数据时,更新

stackoverflow上的第一篇帖子。。。我开始使用SpringMVC,我试图找出将实体尽可能无状态地链接到web视图的最佳方法

我发现的一种方法是在一个方法上使用@ModelAttribute,该方法在参数中接收(来自请求)实体ID,该实体ID从服务/持久性层找到它,并返回它,以便将其插入当前请求的模型中

此外,Spring MVC绑定与my entity字段匹配的任何传入参数,并自动(通过WebDataBinder)更新其值

我的问题是关于最后的行为。我发现当客户端发布了一些数据时,更新我的实体非常有用。但我希望避免在一个简单的GET请求中使用它(我认为它是只读的)。当前行为允许通过在此类请求的查询中添加参数来更新实体,这可能是一个安全问题

我知道dataBinder.setAllowedFields()之类的东西,但我更喜欢一种禁用任何GET请求的任何类型的字段映射的方法。有什么办法吗

谢谢

编辑:我已经添加了一个样本原型,让它更清楚我在寻找什么

@ModelAttribute Entity retrieveEntity(@RequestParam(required=true) Long id) {
    // This is called before the request handler and before parameters are mapped to the entity
    return entityRepository.get(id);
}

@RequestMapping(value="/modify", method=RequestMethod.POST) 
public ModelAndView handleModifyRequest(@ModelAttribute Entity entity) {
    // Here, I want my entity to reflect the parameters passed in the posted form (this works)
    ....
}

@RequestMapping(value="/read", method=RequestMethod.GET) 
public ModelAndView handleReadRequest(@ModelAttribute Entity entity) {
    // Here, I DON'T want my entity to reflect the parameters passed in the URL (which is what happens...)
    ....
}

最后,我决定这样做,因为似乎只有当请求处理程序方法接受ModelAttribute参数时,才会发生参数映射

@ModelAttribute Entity retrieveEntity(@RequestParam(required=true) Long id) {
    return entityRepository.get(id);
}

@RequestMapping(value="/modify", method=RequestMethod.POST) 
public ModelAndView handleModifyRequest(@ModelAttribute Entity entity) {
    // Here, the request parameters have been mapped to the entity
    ....
}

@RequestMapping(value="/read", method=RequestMethod.GET) 
public ModelAndView handleReadRequest(ModelMap model) {
    // This will avoid any parameter mapping to the entity
    Entity entity = (Entity)model.get("entity");
    ....
}

欢迎任何更好的解决方案!谢谢

为什么要将GET请求映射到更新模型的方法?关键是:我不希望它能够更新我的模型。我想使用@modeldattribute方法检索要“获取”的实体。但出于安全目的,我想禁用数据绑定,如果URL中传递了任何与其字段匹配的参数,该绑定将自动更新我的实体(目标)。好的,但为什么不简单地将POST请求映射到实体更改方法,并且在映射到GET请求的方法中不进行实体更新呢?如果您只检索实体并将它们放在方法中的视图中,将不会有更新,对吗?如果我像我所希望的那样使用@ModelAttribute注释方法检索实体,则SpringMVC层会自动应用更新,这是我不想要的。我的想法是创建一个新的注释
@readonlymodeltribute
和一个方面,其中包含控制器方法的@Around建议,并在方面中填充
@readonlymodeltribute
对象。但是——值得花时间吗?