JPA存储库中的findby未找到时是否有任何返回值的方法?

huangapple go评论116阅读模式
英文:

Is there anyway to return value when not found in findby jpa repository

问题

在存储库中创建了一个查询。当未找到值时,我想要响应值。

以下是我的代码:
在存储库中:

  1. Optional<User> findByUsernameAndStatusFalse(String username);

在资源中:

  1. @GetMapping("/user/username/{username}")
  2. public ResponseEntity<User> getUser(@PathVariable String username) {
  3. Optional<User> user = userRepository.findByUsernameAndStatusFalse(username);
  4. return ResponseUtil.wrapOrNotFound(user);
  5. }
英文:

I have create a repository query. I want to response value when not found.

Here are my code:
in repository:

  1. Optional&lt;User&gt; findByUsernameAndStatusFalse(String username);

in resource:

  1. @GetMapping(&quot;/user/username/{username}&quot;)
  2. public ResponseEntity&lt;User&gt; getUser(@PathVariable String username) {
  3. Optional&lt;User&gt; user = userRepository.findByUsernameAndStatusFalse(username);
  4. return ResponseUtil.wrapOrNotFound(user);
  5. }

答案1

得分: 3

java.util.Optional#orElse

如果可选对象没有值,将返回默认值。示例:

User user = userRepository.findByUsernameAndStatusFalse(username).orElse(null);

英文:

java.util.Optional#orElse

this will return a default value if the optional doesnt have a value. example:

User user = userRepository.findByUsernameAndStatusFalse(username).orElse(null);

答案2

得分: 2

public ResponseEntity getUser(@PathVariable String username) {
User userExample = new User();
userExample.setUsername(username);
userExample.setStatus(false);
User user = userRepository.findOne(Example.of(userExample)).orElseThrow(
() -> new RuntimeException("用户名未找到:" + username)
);
return ResponseEntity.ok(user);
}

请将此方法添加到控制器

  1. @ExceptionHandler(RuntimeException.class)
  2. public Map<String, Object> handleException(RuntimeException e) {
  3. Map<String, Object> result = new HashMap<>();
  4. result.put("status", HttpStatus.INTERNAL_SERVER_ERROR.value());
  5. result.put("message", e.getMessage());
  6. return result;
  7. }

当您添加此代码后,如果抛出任何RuntimeException,API将返回状态码500和相应异常的消息。

英文:

You can use :

  1. public ResponseEntity&lt;User&gt; getUser(@PathVariable String username) {
  2. User userExample = new User();
  3. userExample.setUsername(username);
  4. userExample.setStatus(false);
  5. User user = userRepository.findOne(Example.of(userExample)).orElseThrow(
  6. () -&gt; new RuntimeException(&quot;Username not found&quot; + username);
  7. );
  8. return ResponseEntity.ok(user);
  9. }

the above code i use findOne and pass an example to search for an object, when not found throws an exception

please add this method to controller

  1. @ExceptionHandler(RuntimeException.class)
  2. public Map&lt;String,Object&gt; handleException(RuntimeException e) {
  3. Map&lt;String,Object&gt; result = new HashMap&lt;&gt;();
  4. result.put(&quot;status&quot;, HttpStatus.INTERNAL_SERVER_ERROR.value());
  5. result.put(&quot;message&quot;, e.getMessage());
  6. return result;
  7. }

When you add this code, all RuntimeException is thrown then api will return status as 500 and message is messgae of that Exception

huangapple
  • 本文由 发表于 2023年6月16日 12:30:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定