Get or Throw Spring boot Starter will help you to hide handling if entity not found.

Overview

Get or Throw Spring boot Starter

Get or Throw Spring boot Starter will help you to hide handling if entity not found.

1. Setup

2. Usage

Library adds class GetOrThrow like Optional but with errorMessage string and orThrow() method which throw "NotFound" exception with message if GetOrThrow is empty.

If you have Spring-data-jpa dependency in project, get-or-throw-starter will configure converters in Jpa and you can use GetOrThrow in Jpa-repositories.

Example:

findOneById(@Param("id") Long id); }">
interface EntityRepository extends JpaRepository<Entity, Long> {

    @NotFoundExceptionMessage("Not found entity with id = {}")
    @Query("" +
            "SELECT s " +
            "  FROM Entity s " +
            " WHERE s.id = :id ")
    GetOrThrow<Entity> findOneById(@Param("id") Long id);
}

Client code:

    private final EntityRepository entityRepository;

    public Entity getById(Long id){
        return entityRepository.findOneById(id)
                .orThrow();
    }

If you have entity with id then GetOrThrow will have the entity, like Optional method orElseThrow(). If you have not entity with id then GetOrThrow will throw "Not Found" exception (default RuntimeException) with error message from @NotFoundExceptionMessage annotation.

Example:

entityRepository.findOneById(1) -> Entity object with id 1
entityRepository.findOneById(100) -> throw new RuntimeException("Not found entity with id = 100")

NotFoundExceptionMessage

@NotFoundExceptionMessage adds error message in GetOrThrow. You can add {} and param value from method will be inserted in error message.

Example:

findOneById(@Param("id") Long id); ... entityRepository.findOneById(100) .orThrow();">
@NotFoundExceptionMessage("Not found entity with id = {}")
GetOrThrow<Entity> findOneById(@Param("id") Long id);

...

entityRepository.findOneById(100)
    .orThrow();

equals in Optional-style

        
entityRepository.findOneById(100)
    .orElseThrow(() -> new RuntimeException("Not found entity with id = 100"))

With @NotFoundExceptionMessage you can change errorMessage in one place.

You can use GetOrThrow without @NotFoundExceptionMessage. GetOrThrow will throw Exception with default error message Entity not found

GetOrThrowExceptionFactory

In default GetOrThrow will throw RuntimeException. You can change this behavior with GetOrThrowExceptionFactory bean. Create your custom GetOrThrowExceptionFactory implementation and register as bean.

Example:

@Bean
public GetOrThrowExceptionFactory getOrThrowExceptionFactory(){
    return new GetOrThrowExceptionFactory() {
        @Override
        public RuntimeException createException(String s) {
                return new CustomNotFoundException(s);
        }
    };
}
    
and

entityRepository.findOneById(100) -> throw new CustomNotFoundException("Not found entity with id = 100")
You might also like...

Java utilities to throw checked exceptions in a "sneaky" way.

Sneaky Java Java utilities to throw checked exceptions in a "sneaky" way. If you're tired of checked exceptions in lambdas, then this library is made

Dec 3, 2022

An API Library that provides the functionality to access, manage and store device topologies found in JSON files using Java and Maven Framework

An API Library that provides the functionality to access, manage and store device topologies found in JSON files using Java and Maven Framework

Topology API 📙 About An API library which provides the functionality to access, manage and store device topologies. 📝 Description Read a topology fr

Aug 4, 2022

A mod for OneConfig that allows you to customize the hit color of any living entity to your liking.

A mod for OneConfig that allows you to customize the hit color of any living entity to your liking.

HitColor A mod for OneConfig that allows you to customize the hit color of any living entity to your liking. Features An option to toggle Armor being

Nov 5, 2022

Not only Spring Boot but also important knowledge of Spring(不只是SpringBoot还有Spring重要知识点)

Not only Spring Boot but also important knowledge of Spring(不只是SpringBoot还有Spring重要知识点)

在线阅读 : https://snailclimb.gitee.io/springboot-guide (上面的地址访问速度缓慢的建议使用这个路径访问) 重要知识点 基础 Spring Boot 介绍 第一个 Hello World 第一个 RestFul Web 服务 Spring 如何优雅读取配

Jan 3, 2023

Spring Boot starter module for gRPC framework.

Spring Boot starter module for gRPC framework.

Jan 4, 2023

Spring Boot starter module for gRPC framework.

Spring Boot starter module for gRPC framework.

Mar 17, 2021

Spring Boot starter for JustAuth Plus.

Spring Boot starter for JustAuth Plus.

Jun 23, 2022

基于 spring-boot-starter-log4j2:2.6.1 (log4j 2.14.1)

Log4j 2 CVE-2021-44228 测试样本应用 基于 spring-boot-starter-log4j2:2.6.1 (log4j 2.14.1) 可用接口 接口 请求方法 参数 vulnerable_request_get GET v=payload vulnerable_reque

Mar 23, 2022

An awesome Spring Boot Starter!

 An awesome Spring Boot Starter!

spring-boot-tony-starter An awesome Spring Boot Starter! Explore the docs » View Demo · Report Bug · Request Feature Table of Contents About The Proje

Sep 13, 2022
Owner
null
Let Litematica be able to paste tile entity data of block / entity data in a server

Litematica Server Paster Let Litematica be able to paste tile entity data of block / entity data in a server By using a custom chat packet to bypass t

Fallen_Breath 23 Dec 24, 2022
该仓库中主要是 Spring Boot 的入门学习教程以及一些常用的 Spring Boot 实战项目教程,包括 Spring Boot 使用的各种示例代码,同时也包括一些实战项目的项目源码和效果展示,实战项目包括基本的 web 开发以及目前大家普遍使用的线上博客项目/企业大型商城系统/前后端分离实践项目等,摆脱各种 hello world 入门案例的束缚,真正的掌握 Spring Boot 开发。

Spring Boot Projects 该仓库中主要是 Spring Boot 的入门学习教程以及一些常用的 Spring Boot 实战项目教程,包括 Spring Boot 使用的各种示例代码,同时也包括一些实战项目的项目源码和效果展示,实战项目包括基本的 web 开发以及目前大家普遍使用的前

十三 4.5k Dec 30, 2022
Tuya 37 Dec 26, 2022
循序渐进,学习Spring Boot、Spring Boot & Shiro、Spring Batch、Spring Cloud、Spring Cloud Alibaba、Spring Security & Spring Security OAuth2,博客Spring系列源码:https://mrbird.cc

Spring 系列教程 该仓库为个人博客https://mrbird.cc中Spring系列源码,包含Spring Boot、Spring Boot & Shiro、Spring Cloud,Spring Boot & Spring Security & Spring Security OAuth2

mrbird 24.8k Jan 6, 2023
Fall is an app that lets your phone scream if you throw it somewhere.

Fall Fall is an app that lets your phone scream if you throw it somewhere. License Copyright (C) 2022 Gh05t-1337 This program is free software: you ca

null 15 Oct 31, 2022
Create your Java crypto trading bot in minutes. Our Spring boot starter takes care of exchange connections, accounts, orders, trades, and positions so you can focus on building your strategies.

Quick Start | Documentation | Discord | Twitter Create and run your java crypto trading bot in minutes Our Spring boot starter takes care of exchange

Cassandre 442 Jan 3, 2023
A springboot-starter that can achieve Intranet penetration. 一款可以实现内网穿透的springboot-starter。

qynat-springboot-starter 基于netty的内网穿透工具在springboot中的整合 protocol协议:protobuf 只需在application.properties中配置少量信息,实现零代码侵入的web项目内网穿透 项目的server端的源码在另一个多模块项目中,

whz11 65 Dec 12, 2022
A springboot-starter that can achieve Intranet penetration. 一款可以实现内网穿透的springboot-starter。

qynat-springboot-starter 基于netty的内网穿透工具在springboot中的整合 protocol协议:protobuf 只需在application.properties中配置少量信息,实现零代码侵入的web项目内网穿透 项目的server端的源码在另一个多模块项目中,

whz11 65 Dec 12, 2022
A boilerplate project designed to work as a template for new microservices and help you get in touch with various useful concepts.

Microservice Reference Project This project is inspired by the idea to quickly create a production ready project with all the required infrastructure

Innovation & Tech 4 Dec 17, 2022
Kafka-spring-boot-starter: encapsulated based on spring-kafka

Encapsulation based on spring-kafka not only supports native configuration, but also adds multi data source configuration.

liudong 8 Jan 9, 2023