我有一个带有类的简单POJO对象:
public class Place
{
@NotNull
private String country;
@NotNull
private String city;
}
如何在此处将此类对象绑定到RequestParam:
public ResponseEntity find(@RequestParam String date, Place departure, Place arrival)
使用api:
/ find?date = 2019-02-10T16:00:00&country = Russia&city = Samara&country = Russia&city = Moscow
使用获得响应
System.out.println(departure);
System.out.println(arrival);
地点{country ='Russia,Russia',city ='Samara,Moscow'}
地点{country ='Russia,Russia',city ='Samara,Moscow'}
我接下来可以得到答复吗?
地点{country ='Russia',city ='Samara'}
地点{country ='俄罗斯',city ='莫斯科'}
答案 0 :(得分:1)
问题在于,Spring无法区分具有相同名称的参数,并将其堆叠。 对于像这样的小型业务对象,我将在特定的参数对象中显式创建一个不同的名称。
public void find(@RequestParam String date, Travel travel)
使用这样的对象:
public class TravelDto {
@NotNull
private String fromCountry;
@NotNull
private String fromCity;
@NotNull
private String toCountry;
@NotNull
private String toCity;
....
}
对于您的情况,从HTTP cal的角度来看,这将使事情变得清楚:
/ find?date = 2019-02-10T16:00:00&fromCountry =俄罗斯&fromCity = Samara&toCountry = Russia&toCity =莫斯科