简体   繁体   中英

How to post a list of object using JAX-RS (to perform bean validation)

How can I post a list of items using application/x-www-form-urlencoded content type ?

For example, I would like to send a List of :

public class Person {
  @NotNull
  private String name;
  private int age;
  // getter/setter...
}

And this is my rest service definition :

@POST
@Path("/persons")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String createPersons(@Valid @FormParam("schedules") List<Person> persons) {
    return "OK";
}

But it seems not being accepted by jersey (no injection...). How can I pass non-primitive list of data with JAX-RS ?

Post data looks like that : persons[0][name]=Test&persons[0][age]=45&persons[1][name]=Test2&persons[1][age]=22

You can use @BeanParam on your @POST Listener.

public String createPersons(@Valid @BeanParam List<Person> persons) {
  return "OK";
}

But you have to make sure your Person has @FormParam annotation for each fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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