【问题标题】:How to get mulitiple lists of objects from a REST controller?如何从 REST 控制器获取多个对象列表?
【发布时间】:2019-01-17 10:12:42
【问题描述】:

我需要从休息控制器获取两个不同类型对象的两个列表作为参数,它正在向我发送一个

"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"

我的控制器的一部分:

 @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);

}

我的分类对象(位置对象是同一类型):

@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;

所以我想知道有没有办法让控制器知道我想将参数作为对象列表而不是字符串?

【问题讨论】:

    标签: java spring-mvc controller


    【解决方案1】:
    1. 定义模型类
    2. 在其中定义一个数组列表。
    3. 使用@RequestBody 注释,如下所示:-

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

      public ResponseEntity> getProjects(@ResponseBody ModelClass model) {

      }

    【讨论】:

      猜你喜欢
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-05
      • 2016-05-12
      • 2014-01-12
      • 1970-01-01
      相关资源
      最近更新 更多