Spring Boot – 抛出异常还是指示项未找到

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

Spring Boot - throw exception or indicate item is not found

问题

我写了两个方法,findById 在数据库中查找项目,如果未找到该项目,则抛出异常:

public Url findById(final Long id){
        return urlRepository.findById(id)
                .orElseThrow(() -> new ShortUrlNotFoundException("找不到给定ID的URL"));
}

第二个方法,findByShortUrl 在数据库中查找项目,并使用 JPA 方法 findByShortUrlIs,如果找到该项目,则返回大小为1的列表;对于给定的 shortUrl,在数据库中永远不应该有多于1个的项目:

public Optional<String> findByShortUrl(final String shortUrl){
    
    List<Url> urlList = urlRepository.findByShortUrlIs(shortUrl);
    if(urlList.isEmpty()){
        return Optional.empty();
    }
    else {
        return Optional.of(urlList.get(0).getLongUrl());
    }
}

我喜欢在项目未找到时使用 ShortUrlNotFoundException 的模式。那么,是否应该在 findByShortUrl 中也使用它?这样,findByShortUrl 变成了:

public Optional<String> findByShortUrl(final String shortUrl){
    
    List<Url> urlList = urlRepository.findByShortUrlIs(shortUrl);
    if(urlList.isEmpty()){
        throw new ShortUrlNotFoundException("找不到给定ID的URL");
    }
    else {
        return Optional.of(urlList.get(0).getLongUrl());
    }
}
英文:

I've written two methods, findById searches for an item in the DB and throws an exception if the item is not found :

public Url findById(final Long id){
        return urlRepository.findById(id)
                .orElseThrow(() -&gt; new ShortUrlNotFoundException(&quot;URL not found for the given ID&quot;));
}

The second method, findByShortUrl searches for an item in the DB and uses the JPA method findByShortUrlIs which returns a List of size 1 if the item is found, there should never be more than 1 item in the DB for a given shortUrl :

public Optional&lt;String&gt; findByShortUrl(final String shortUrl){

    List&lt;Url&gt; urlList = urlRepository.findByShortUrlIs(shortUrl);
    if(urlList.isEmpty()){
        return Optional.empty();
    }
    else {
        return Optional.of(urlList.get(0).getLongUrl());
    }
}

I like the pattern of using a ShortUrlNotFoundException if an item is not found. Should I use it also in findByShortUrl ? Then, findByShortUrl becomes:

public Optional&lt;String&gt; findByShortUrl(final String shortUrl){

    List&lt;Url&gt; urlList = urlRepository.findByShortUrlIs(shortUrl);
    if(urlList.isEmpty()){
        throw new ShortUrlNotFoundException(&quot;URL not found for the given ID&quot;)
    }
    else {
        return Optional.of(urlList.get(0).getLongUrl());
    }
}

答案1

得分: 1

为什么不使用以下方式的findFirst

Optional<Url> findFirstByShortUrlIs(String shortUrl);

然后,您调用:

public Optional<String> findByShortUrl(final String shortUrl){
    return urlRepository.findFirstByShortUrlIs(shortUrl)
            .map(Url::getLongUrl)
            .map(Optional::of)
            .orElseThrow(() -> new ShortUrlNotFoundException("找不到给定 ID 的 URL"));
}
英文:

Why not using findFirst as this:

Optional&lt;Url&gt; findFirstByShortUrlIs(String shortUrl);

and then, you call:

public Optional&lt;String&gt; findByShortUrl(final String shortUrl){
        return urlRepository.findFirstByShortUrlIs(shortUrl)
                .map(Url::getLongUrl)
                .map(Optional::of)
                .orElseThrow(() -&gt; new ShortUrlNotFoundException(&quot;URL not found for the given ID&quot;));
}

答案2

得分: 1

个人而言,我绝不会在这两种情况下使用异常机制。异常设计用于未被常规业务逻辑覆盖的意外情况。例如,findById 应在仅在数据库中不期望存在该项时抛出异常进行搜索。对于 findByShortUrl 也是同样的情况。据我所理解,在数据库中没有对象是一种正常情况,因此预期应该是结果为空的情况。另一方面,你提到了“数据库中不应该有多于1个项目”,这正是使用异常的完美情况!如果发现返回的结果集中有两个以上的对象,那就抛出异常。

英文:

Personally speaking, I would never use exception mechanism for both cases. Exceptions are designed for unexpected situations that aren't covered by conventional business logic. For example, findById should throw an exception for the search if and only if you do not expect that item in the database. The same story for the findByShortUrl. As far as I understand it's a normal case not to have a object in db, thus it should be expected case to have empty result. On other hand, you've mentioned that "there should never be more than 1 item in the DB", and that's the perfect case for an exception! If you see that return result set has more than 2 objects, so go and thrown an excpetion.

huangapple
  • 本文由 发表于 2020年9月2日 15:46:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63700961.html
匿名

发表评论

匿名网友

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

确定