如何告诉Spring不要绑定对象属性?

huangapple go评论71阅读模式
英文:

How do I tell Spring to exclude binding an object property?

问题

我有一个Spring控制器方法,有两个对象参数:

@GetMapping
public List<Car> getCars(BlueBookCar blueBookCar, AutoTraderCar autoTraderCar)

BlueBookCarAutoTraderCar都有一个名为id的字段。我希望在HTTP GET请求中,查询参数名id只绑定到BlueBookCar实例。

例如,以下请求:

http://localhost/cars?id=123

应该导致blueBookCar具有id等于123。而autoTraderCar的id应该为null。但是,Spring设置了两个对象的id都为123。

我该如何告诉Spring排除绑定autoTraderCarid?其他与查询参数名匹配的autoTraderCar字段应该设置(如Spring通常所做的)。

更新

我想要注意:

  1. 我不希望HTTP客户端知道存在两种类型的汽车,BlueBookCarAutoTraderCar
  2. 除了id之外,两种汽车类型的所有属性都不同。以后可能会有其他属性重叠。
  3. 出于维护原因和类之间的关系,我不想创建一个包含两个类属性集的第三个类。

ASP.NET MVC支持属性绑定排除。请参阅https://stackoverflow.com/a/8332917/1706691。Spring是否支持这个?

英文:

I have a Spring controller method that has two object params:

@GetMapping
public List&lt;Car&gt; getCars(BlueBookCar blueBookCar, AutoTraderCar autoTraderCar)

Both BlueBookCar and AutoTraderCar have a field named id. I want the query param name id in an HTTP GET request to be bound only to the BlueBookCar instance.

For example, the following:

http://localhost/cars?id=123

Should result in blueBookCar having an id equal to 123. The autoTraderCar id should be null. Instead, Spring sets both objects with an id of 123.

How can I tell Spring to exclude binding id for the autoTraderCar? Other autoTraderCar fields that have names matching query param names should be set (as Spring normally does).

Update

I meant to note that:

  1. I do not want HTTP clients to be aware of the existence of two types of cars, BlueBookCar & AutoTraderCar
  2. All properties except id are different between the two types of car. It's possible that later other properties may overlap.
  3. I do not want to create a third class with the set of properties from both classes for maintenance reasons and because the classes have relationships.

ASP.NET MVC supports property binding exclusions. See https://stackoverflow.com/a/8332917/1706691. Does Spring support this?

答案1

得分: 1

你可以使用@InitBinder来访问WebDataBinder,以自定义如何将HTTP请求中的数据绑定到控制器方法中的特定参数对象。

在你的情况下,添加以下内容应该解决你的问题:

@Controller
public class CarController {

    @InitBinder("autoTraderCar")
    public void initAutoTraderCarDataBinder(WebDataBinder binder) {
        binder.setDisallowedFields("id");
    }

    @GetMapping
    public List<Car> getCars(BlueBookCar blueBookCar, AutoTraderCar autoTraderCar) {

    }

}
英文:

You can use @InitBinder which allows you to access WebDataBinder for customising how to bind the data from the http request to a particular parameter object in the controller method.

In your case , adding the following should solve your problem :

@Controller
public class CarController {


    @InitBinder(&quot;autoTraderCar&quot;)
    public void initAutoTraderCarDataBinder(WebDataBinder binder) {
        binder.setDisallowedFields(&quot;id&quot;);
    }


    @GetMapping
    public List&lt;Car&gt; getCars(BlueBookCar blueBookCar, AutoTraderCar autoTraderCar){

    }

}


huangapple
  • 本文由 发表于 2023年7月7日 06:18:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定