【发布时间】:2018-05-19 20:14:05
【问题描述】:
您能帮我解释一下我应该如何传递作者值吗?
@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 类看起来像:
@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;
}
在这种情况下使用作者或@RequestParam 代替请求正文是否是一种好方法? 我考虑过只请求作为作者姓名的字符串,但这会影响服务的方法。
【问题讨论】:
-
您不能使用作者 ID 代替完整作者吗?
标签: java spring spring-mvc model-view-controller