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

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

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

问题

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

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

Optional<User> findByUsernameAndStatusFalse(String username);

在资源中:

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

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

Here are my code:
in repository:

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

in resource:

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

答案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);
}

请将此方法添加到控制器

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

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

英文:

You can use :

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

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

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

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:

确定