[英]Sending List of objects to Rest service in Java
I have a rest api that looks like this 我有一个看起来像这样的REST API
@POST
@Path("/cities")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response createCity(City city) {
}
Above is working fine.Now I want to have another service that accepts List of Cities. 上面的工作正常,现在我想再接受一个接受城市名单的服务。 Then I created a wrapper object which has List of cities field, something like this 然后,我创建了一个包装对象,其中包含“城市列表”字段,如下所示
@XmlRootElement
public class CityHolder {
List<City> cities;
....................
@POST
@Path("/cities/list")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response createCities(CityHolder cityHolder) {
}
Above worked fine for me. 以上对我来说很好。 I tried below also 我也在下面尝试
@POST
@Path("/cities/list")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response createCities(List<City> cityList) {
}
which works fine as well. 效果也很好。 Can someone let me know what's the best practice to send the list of objects. 有人可以让我知道发送对象列表的最佳做法是什么。
I would say if you only want to send the list, use a list of objects. 我想说的是,如果您只想发送列表,请使用对象列表。 If you want to send additional hints with the list, use a wrapped object. 如果要与列表一起发送其他提示,请使用包装的对象。
In your case, I would prefer the list ifself. 在您的情况下,我更希望列出该列表。
Hope it helps, Thierry 希望对您有帮助,蒂埃里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.