Skip to content

Instantly share code, notes, and snippets.

@andersonbosa
andersonbosa / add exception handler to spring boot.md
Last active February 14, 2025 18:26
add exception handler to spring boot

file: infra/GlobalExceptionHandler.java

@ControllerAdvice
public class GlobalExceptionHandler {

 @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<?> handleJakartaValidations(MethodArgumentNotValidException e, HttpServletRequest r) {
 Map errors = new HashMap&lt;&gt;();
CREATE TABLE cliente (
id INT NOT NULL,
nome VARCHAR(45) NOT NULL,
sobrenome VARCHAR(45) NOT NULL,
idade INT NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE carro (
id INT NOT NULL,
-- schema do banco
CREATE TABLE IF NOT EXISTS departamento (
id INT NOT NULL AUTO_INCREMENT,
nome VARCHAR(128) NOT NULL,
sobrenome VARCHAR(128) NOT NULL,
localizacao VARCHAR(255) NOT NULL,
PRIMARY KEY(id)
);
CREATE TABLE cliente (
dni VARCHAR(255),
nome VARCHAR(255),
sobrenome VARCHAR(255),
id BIGINT UNIQUE NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
);
INSERT INTO cliente (dni,nome,sobrenome)
VALUES
@andersonbosa
andersonbosa / java.formatter.xml
Last active January 19, 2025 19:52
My custom formatter to Java in vscode
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<profiles version="13">
<profile kind="CodeFormatterProfile" name="GoogleStyle" version="13">
<setting id="org.eclipse.jdt.core.formatter.comment.insert_new_line_before_root_tags" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.disabling_tag" value="@formatter:off"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_annotation" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_parameters" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_type_declaration" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_arguments" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_anonymous_type_declaration" value="end_of_line"/>
@andersonbosa
andersonbosa / VehicleRepositoryImpl.java
Last active January 14, 2025 19:53
Implementation of "update" method in a "in memory" repository.
@Override
public Vehicle update(Long id, UpdateVehicleDto dto) {
Vehicle vehicleToUpdate = findById(id).orElse(null);
if (vehicleToUpdate == null) {
return null;
}
// method 1
if (dto.getBrand() != null) vehicleToUpdate.setBrand(dto.getBrand());
if (dto.getModel() != null) vehicleToUpdate.setModel(dto.getModel());
if (dto.getRegistration() != null) vehicleToUpdate.setRegistration(dto.getRegistration());
// ==UserScript==
// @name automute youtube on video ads
// @namespace http://tampermonkey.net/
// @description try to take over the world!
// @author You
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// ==/UserScript==
@andersonbosa
andersonbosa / wordpress-server-block.conf
Last active December 10, 2024 21:18
NGINX conf to deploy a Wordpress from bitnami in a basepath "/blog"
server {
listen 8080 default_server;
listen [::]:8080 default_server;
server_name _;
proxy_set_header True-Client-IP $http_true_client_ip;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
@andersonbosa
andersonbosa / open_addressing_in_hash_table_example.ts
Last active October 24, 2024 10:23
open_addressing_exmaple.ts
/*
Como funcionam as estratégias de "encadeamento" e "endereçamento aberto"?
> No encadeamento, as colisões são resolvidas armazenando múltiplos itens no mesmo índice (geralmente com uma lista ligada). No endereçamento aberto, novas posições são exploradas dentro do array para armazenar o item colidido.
Endereçamento aberto (Open Addressing) é uma técnica utilizada em estruturas de dados como tabelas de dispersão (hash tables) para lidar com colisões.
Quando ocorre uma colisão, ou seja, dois elementos diferentes têm o mesmo endereço de hash, o endereçamento aberto procura novas posições dentro do array para armazenar o item colidido.