Skip to content

Instantly share code, notes, and snippets.

View kapresoft's full-sized avatar

Tony Lagnada (Kapresoft) kapresoft

View GitHub Profile
@kapresoft
kapresoft / org.springframework.core.convert.support.StringToNumberConverterFactory
Last active January 3, 2024 00:43
ConversionService Article: /java/2024/01/02/spring-conversion-service-converting-lists.html
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
package com.kapresoft.api;
import com.kapresoft.api.exception.client.BadRequestException;
import com.kapresoft.api.exception.client.NotFoundException;
import org.springframework.http.ProblemDetail;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.client.HttpClientErrorException;
@ExceptionHandler(BadRequestException.class)
public ResponseEntity<HttpClientErrorException> handleBadRequest(BadRequestException ex, WebRequest req) {
ProblemDetail p = ProblemDetail.forStatus(ex.getStatusCode());
URI type = new DefaultUriBuilderFactory()
.uriString("https://yoursite.acme.com/bad-request/{error-code}")
.build(Map.of("error-code", ex.getErrorCode()));
p.setDetail(ex.getStatusText());
p.setTitle("Bad Request");
p.setType(type);
p.setProperty("hint", "hint-value");
@ExceptionHandler(NotFoundException.class)
public ResponseEntity<HttpClientErrorException> handleNotFound(NotFoundException ex, WebRequest req) {
ProblemDetail p = ProblemDetail.forStatus(ex.getStatusCode());
URI type = new DefaultUriBuilderFactory()
.uriString("https://yoursite.acme.com/not-found/{error-code}")
.build(Map.of("error-code", ex.getErrorCode()));
p.setDetail(ex.getStatusText());
p.setTitle("Object not found");
p.setType(type);
p.setProperty("hint", "hint-value");
@kapresoft
kapresoft / Singular.java
Created May 1, 2023 23:50
Lombok Singular
package lombok;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This annotation can be used on a field of a builder class to let lombok generate an add method for adding a single
* element to the collection behind the field.
@kapresoft
kapresoft / account-service.uml
Last active April 12, 2023 21:07
Spring Boot Jackson and Lombok Best Practices - Account UML
@startuml
"Client App" -> "Account API": POST /account
"Account API" -> "Jackson ObjectMapper": readValue(byte[] src, Class<Account>)
"Account API" -> "Account API": createAccount(Account)
"Account API" -> "Jackson ObjectMapper": writeValue(OutputStream out, Account newAccount)
"Client App" <-- "Account API": Response (OutputStream)
@enduml
; AutoHotKey: Mac key bindings for IntelliJ/Android Studio for Windows
#IfWinActive ahk_exe idea64.exe || ahk_exe studio64.exe
; ====== COMMON ========================================
; Find Action (⌘+Shift+A) -> (Ctrl+Shift+A)
#+a::
{
Send ^+a
@kapresoft
kapresoft / spring-boot-dependencies-2.6.1.pom
Created December 13, 2021 20:11
Spring Boot Dependencies 2.6.1 POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.6.1</version>
<packaging>pom</packaging>
<name>spring-boot-dependencies</name>
<description>Spring Boot Dependencies</description>
<url>https://spring.io/projects/spring-boot</url>
@kapresoft
kapresoft / Account.java
Last active November 3, 2021 22:55
Immutable Account using Lombok `@Value` Annotation
package com.kapresoft.springboot.serializeimmutableobjects.dto.simple;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Builder;
import lombok.Value;
/**
* Immutable Account using Lombok {@code @Value}.
*/
@kapresoft
kapresoft / AccountWithoutUsingValue.java
Last active November 3, 2021 22:54
Immutable Account without using Lombok `@Value` Annotation
package com.kapresoft.springboot.serializeimmutableobjects.dto.simple;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.*;
import lombok.experimental.FieldDefaults;
@Getter
@ToString
@EqualsAndHashCode