简体   繁体   中英

How to get mulitiple lists of objects from a REST controller?

I need to get two lists of two different types objects as parameters from a rest controller and it's sending me a

"error": "Internal Server Error",

"message": "Failed to convert value of type 'java.lang.String' to required type 'java.util.List';
nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type
'com.nord.execom.domain.Category': no matching editors or conversion strategy found"

Part of my controller:

 @RequestMapping(
        value = "/projects",
        params = {"category", "location"},
        method = GET)
@ResponseBody
public ResponseEntity<List<Project>> getProjects(@RequestParam("category") List<Category> category,
                                                 @RequestParam("location") List<Location> location) {
    List<Project> project = projectService
            .getProjects(category, location);

    return ResponseEntity.ok().body(project);

}

My category object(location object is the same type):

@Entity
@Table(name = "category")
public class Category {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int Id;

@NotBlank
@Column(unique = true)
@Size(min = 1, max = 50)
private String name;

@OneToMany(mappedBy = "category")
private List<Project> project;

So i was wondering is there a way to let the controller know that i want to take the parameters as a list of objects and not Strings?

  1. Define a model class
  2. Define an arraylist in it.
  3. Use @RequestBody annotation like below: -

     ModelClass{ List<String> list; //Getters and setter for the attribute } 

    public ResponseEntity> getProjects(@ResponseBody ModelClass model) {

    }

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