San*_*omo 2 java response optional java-8 spring-boot
我正在学习 Spring Boot,当服务没有在数据库中找到项目时,我试图抛出异常,因此,我尝试使用 optional 但当我测试它时,除了异常之外,我只得到一个空响应
@GetMapping(value = "/compras", produces = "application/json")
public Optional<Compras> retrieveAllCompras(@RequestParam String id) {
return Optional.of(compraRepository.findById(id)).orElseThrow(RuntimeException::new);
Run Code Online (Sandbox Code Playgroud)
当在数据库中找不到该项目时,我预计会出现异常
Optional.of期待纯粹的价值。您也可以在文档中找到信息,
/**
* Constructs an instance with the described value.
*
* @param value the non-{@code null} value to describe
* @throws NullPointerException if value is {@code null}
*/
private Optional(T value) {
this.value = Objects.requireNonNull(value);
}
Run Code Online (Sandbox Code Playgroud)
例子,
jshell> Optional.of(100)
$2 ==> Optional[100]
jshell> Optional.of(null)
| Exception java.lang.NullPointerException
| at Objects.requireNonNull (Objects.java:221)
| at Optional.<init> (Optional.java:107)
| at Optional.of (Optional.java:120)
| at (#1:1)
Run Code Online (Sandbox Code Playgroud)
如果您的价值可能null在运行时,您可以使用.ofNullable,
jshell> Optional.ofNullable(null)
$3 ==> Optional.empty
Run Code Online (Sandbox Code Playgroud)
ALSO
函数式编程的思想是为所有输入返回一个值,而不是抛出Exception会破坏函数组合。
jshell> Function<Integer, Optional<Integer>> f = x -> Optional.of(x + 1)
f ==> $Lambda$23/0x0000000801171c40@6996db8
jshell> Function<Integer, Optional<Integer>> g = x -> Optional.of(x * 2)
g ==> $Lambda$24/0x0000000801172840@7fbe847c
jshell> f.apply(5).flatMap(x -> g.apply(x))
$13 ==> Optional[12]
Run Code Online (Sandbox Code Playgroud)
因此,在您的示例中,您可以将Optional.empty()item not found 视为 item not found,但 Spring 也会考虑这一点,200这仍然比 throwing 更好500。您可能希望发送404准确无误。
@GetMapping(
value = "/compras",
produces = "application/json"
)
public Optional<Compras> retrieveAllCompras(@RequestParam String id) {
return Optional.ofNullable(compraRepository.findById(id)); //will response as 200 even when no item found
}
Run Code Online (Sandbox Code Playgroud)
您可以使用ResponseEntity<A>设置特定的 http 状态
传统的响应方式404是定义特定的异常。
import org.springframework.web.server.ResponseStatusException;
import org.springframework.http.HttpStatus;
@GetMapping(
value = "/compras",
produces = "application/json"
)
public Compras retrieveAllCompras(@RequestParam String id) {
return Optional.ofNullable(compraRepository.findById(id))
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "item not found"))
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5334 次 |
| 最近记录: |