【问题标题】:Spring Boot Rest API-Invalid Request Param Exception : java.lang.IllegalArgumentException: Parameter value [Computer] did not match expected typeSpring Boot Rest API-无效请求参数异常:java.lang.IllegalArgumentException:参数值[计算机]与预期类型不匹配
【发布时间】:2023-04-11 12:28:01
【问题描述】:

如何从OneToMany实体实现搜索?我有Book andCategory` 域模型。

public class Category {
   private long id;
   private String name;
   @OneToMany(mappedBy = "category", cascade = CascadeType.ALL)
   private Set<Book> books;
}

public class Book {
   private long id;
   private String title;
   @ManyToOne
   @JoinColumn(name = "category_id")
   private Category category;
}

这是我在控制器中的方法:

@GetMapping("/books")
public String findAll(Model model, @RequestParam(value = "category", required = false) String category) {
   if (category != null && !category.isEmpty()) {
      model.addAttribute("books", bookService.findByCategory(category));
   } else {
      model.addAttribute("books", bookService.findAll());
   }

   return "book/findAll";
}

我的服务:

@Override
public Iterable<Book> findByCategory(String category) {
   return bookRepository.findByCategory(category);
}

还有我的仓库:

Iterable<Book> findByCategory(String category);

请求/books/?category=Computer,浏览器出现以下错误:

There was an unexpected error (type=Internal Server Error, status=500).
Parameter value [Computer] did not match expected type [io.github.julianjupiter.springbootjpamanytoone.domain.Category (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [Computer] did not match expected type [io.github.julianjupiter.springbootjpamanytoone.domain.Category (n/a)]

谢谢。

【问题讨论】:

  • 我觉得上个标题好多了。

标签: java spring-boot spring-data-jpa


【解决方案1】:

删除/之后

书籍

并在您的存储库中尝试

 Iterable <Book> findByCategoryName(String categoryName);

【讨论】:

  • 谢谢。它可以工作,但应该是findByCategoryName。
  • 哦,显然应该是名称而不是标题。我只是忽略了它。我现在已经更新了我的答案。
【解决方案2】:

查询参数的使用方式如下:

/books?category=Computer

删除/

【讨论】:

  • 我觉得是一样的。
【解决方案3】:

如果您看到您的请求映射方法具有 @RequestParam 而不是 @PathVariable。

@GetMapping("/books")
public String findAll(Model model,@RequestParam(value = "category", required = false) String category) {
...
...
...
// Your Code
}

因此,提出具有 QueryParam / RequestParam 的请求的正确方法是

/books?category=Computer // this request enables to get the service /books with request param (name = category) with value = Computer.

不是这样的

/books/?category=Computer  // this request is a invalid request.

如果你的映射是这样的

    @GetMapping("/books/category/{categoryName}")
    public String findAll(Model model,@PathVariable("categoryName") String category) {
    ...
    ...
    ...
    // Your Code
    }

那么这个请求对于这个 Rest-API 是有效的

/books/category/Computer  // this request is a valid now.

【讨论】:

  • 如果我们遵循 REST API 的命名约定,它应该是/books、/books/{id}、/books/{id}/categories、/books/{bookId}/categories/{categoryId}
  • 但是我不能在BookController下应用它,因为目的是搜索所有类别与categoryRequestParam的值相同的书籍。它应该在CategoryController下。
猜你喜欢
  • 2019-02-11
  • 2017-03-04
  • 2019-07-23
  • 1970-01-01
  • 2015-12-13
  • 2020-02-23
  • 2020-08-11
  • 2015-08-17
  • 1970-01-01
相关资源
最近更新 更多