Skip to content

Instantly share code, notes, and snippets.

@Christian-Oette
Christian-Oette / CountryCodeValidator.java
Created November 29, 2022 14:05
JSR 303 Country code validator with java locale
public class CountryCodeValidator implements ConstraintValidator<ValidIso2CountryCode, String> {
private static final Set<String> isoCountries = Locale.getISOCountries(Locale.IsoCountryCode.PART1_ALPHA2);
@Override
public boolean isValid(String content, ConstraintValidatorContext context) {
if (content == null) {
setErrorMessage(context, "Country code must not be null");
return false;
}
@Christian-Oette
Christian-Oette / FileResponseResource.java
Last active December 22, 2022 13:37
Return a csv file from spring
public class FileResponseResource extends ByteArrayResource {
private final String fileName;
private final int fileLength;
public FileResponseResource(String fileName, final byte[] byteArray) {
super(byteArray);
this.fileName = Objects.requireNonNull(fileName);
this.fileLength = byteArray.length;
}
@Christian-Oette
Christian-Oette / CustomErrorController.class
Last active October 12, 2023 08:36
Override BasicErrorController in spring boot to disable whitelabel page and to remove the default error return
import java.util.Map;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Christian-Oette
Christian-Oette / OpenApiNamingConventionTest.class
Created October 17, 2023 10:33
Programatically test the naming convention of OpenAPI in spring boot
import java.util.regex.Pattern;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.models.OpenAPI;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;