Java 如何在URI行中将对象作为查询参数传递?

Java 如何在URI行中将对象作为查询参数传递?,java,spring,spring-mvc,model-view-controller,Java,Spring,Spring Mvc,Model View Controller,你能帮我解释一下我应该如何传递作者值吗 @GetMapping(value = "/search") public ResponseEntity<List<Book>> searchBooksByTitleAndAuthor( @RequestParam(value = "title", required = false) final String title, @RequestParam(value = "auth

你能帮我解释一下我应该如何传递作者值吗

@GetMapping(value = "/search")
    public ResponseEntity<List<Book>> searchBooksByTitleAndAuthor(
            @RequestParam(value = "title", required = false) final String title,
            @RequestParam(value = "author", required = false) final Author author) {
        HttpStatus httpStatus = HttpStatus.OK;
        List<Book> books = null;
        if (title == null && author == null) {
            log.info("Empty request");
            httpStatus = HttpStatus.BAD_REQUEST;
        } else if (title == null || author == null) {
            books = bookService.getBooksByTitleOrAuthor(title, author);
        } else {
            Optional<Book> book = bookService.getBookByTitleAndAuthor(title, author);
            if (book.isPresent()) {
                books = Arrays.asList(book.get());
            }
        }
        if (books == null) {
            return new ResponseEntity<>(httpStatus);
        } else {
            return new ResponseEntity<>(books, httpStatus);
        }
    }
在这种情况下,使用author或@RequestParam而不是request body是一种好方法吗? 我曾考虑只请求作为作者姓名的字符串,但这会影响服务的方法。

根据。。。您可以(在
作者.dateOfBirth
上设置一些转换注释后):

  • 对字符串参数使用
    @RequestParam
    (并让您的控制器/用户 进行转换):

    在这种情况下,您会要求:

    http://localhost:8080/myApp/search?title=foo&author={"id"="1",...}
    
    http://localhost:8080/myApp/search?title=foo&id=1&name=Donald E. Knuth&...
    
  • 或者:省略
    @RequestParam
    ,但传递对象(并让spring负责转换):

    并要求:

    http://localhost:8080/myApp/search?title=foo&author={"id"="1",...}
    
    http://localhost:8080/myApp/search?title=foo&id=1&name=Donald E. Knuth&...
    
  • 另见:

    • 根据。。。您可以(在
      作者.dateOfBirth
      上设置一些转换注释后):

    • 对字符串参数使用
      @RequestParam
      (并让您的控制器/用户 进行转换):

      在这种情况下,您会要求:

      http://localhost:8080/myApp/search?title=foo&author={"id"="1",...}
      
      http://localhost:8080/myApp/search?title=foo&id=1&name=Donald E. Knuth&...
      
    • 或者:省略
      @RequestParam
      ,但传递对象(并让spring负责转换):

      并要求:

      http://localhost:8080/myApp/search?title=foo&author={"id"="1",...}
      
      http://localhost:8080/myApp/search?title=foo&id=1&name=Donald E. Knuth&...
      
    • 另见:


      您不能使用作者ID而不是完整作者吗?您不能使用作者ID而不是完整作者吗?