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
@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");
@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");
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;
@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 / 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 / HierarchicalAccount.java
Last active November 3, 2021 22:50
Immutable HierarchicalAccount using Lombok `@Value` Annotation
package com.kapresoft.springboot.serializeimmutableobjects.dto.hierarchical;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Value;
@Value
@EqualsAndHashCode(callSuper = true)
@kapresoft
kapresoft / BaseAccount.java
Last active November 3, 2021 22:51
Immutable BaseAccount using Lombok
package com.kapresoft.springboot.serializeimmutableobjects.dto.hierarchical;
import lombok.Getter;
@Getter
public abstract class BaseAccount {
String username;
String email;
@kapresoft
kapresoft / jackson-bom-2.12.5.pom
Created November 2, 2021 04:11
Spring 2.5.x Jackson BOM file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-parent</artifactId>
<!-- note: does NOT change for every version of bom -->
<version>2.12</version>
</parent>
@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