简体   繁体   English

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

[英]How can I pass an object as a query param in URI line?

Could you please help me and explain how should I pass an author value? 您能否帮助我,并解释我应该如何传递作者价值?

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

And Author class that looks like: Author类看起来像:

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
public final class Author {

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

    private String name;

    private LocalDate dateOfBirth;

    private String bio;
}

Is it a good approach to use author or @RequestParam instead of request body in this situation? 在这种情况下,使用author或@RequestParam代替请求正文是一种好方法吗? I've thought about requesting only String that is authors' name but it will affect service's methods. 我考虑过只请求作者姓名的String,但这会影响服务的方法。

According to https://lankydanblog.com/2017/03/11/passing-data-transfer-objects-with-get-in-spring-boot/ , ... you can (after you set some conversion annotations on your author.dateOfBirth ): https://lankydanblog.com/2017/03/11/passing-data-transfer-objects-with-get-in-spring-boot/ ,...你可以(在你设置你的一些转换注释author.dateOfBirth ):

  1. Use @RequestParam on a String parameter (and let your controller/someone do the conversion): 在String参数上使用@RequestParam (并让您的控制器/某人进行转换):

      ..., @RequestParam(value = "author", required = false) final String author) { ...final Author author = new ObjectMapper().setDateFormat(simpleDateFormat) .readValue(author, Author.class); 

    In this case you would request like: 在这种情况下,您会要求:

     http://localhost:8080/myApp/search?title=foo&author={"id"="1",...} 
  2. alternatively: Omit @RequestParam , but pass the object (and let spring care for conversion): 或者:省略@RequestParam ,但传递对象(并让spring进行转换):

     ...(@RequestParam(value = "title", required = false) final String title, final Author author) 

    and request like: 并要求像:

     http://localhost:8080/myApp/search?title=foo&id=1&name=Donald E. Knuth&... 

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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