简体   繁体   English

使用Spring Boot批量创建API

[英]Bulk create api with spring boot

I'm trying to come up with the proper way to create an end point that gives the option to create a single object or a list of it. 我正在尝试提出一种创建端点的正确方法,该端点提供了创建单个对象或列表的选项。 I'm using spring-boot framework. 我正在使用spring-boot框架。 How do I offer both options with the same end point? 如何为两个选项提供相同的终点? and also is the proper way to do it with the same end point name? 并且也是使用相同终点名称的正确方法吗?

Thanks! 谢谢!

@RequestMapping(value = "/persons", method = RequestMethod.POST)
public void save(@RequestBody() final Person person)

or 要么

@RequestMapping(value = "/persons", method = RequestMethod.POST)
public void save(@RequestBody() final List<Person> persons)

You can treat the single object as a list with one element, and keep that handler method. 您可以将单个对象视为具有一个元素的列表,并保留该处理程序方法。 That's a little challenging for the client though, but if they don't care, it's the simplest option. 虽然对客户来说这是一个挑战,但是如果他们不在乎,那是最简单的选择。 Otherwise, you can accept the request body as a string and try to figure out whether it's meant to be a single object or a list and act accordingly. 否则,您可以接受请求主体作为字符串,并尝试弄清楚它是单个对象还是列表,并采取相应的措施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM