Skip to content

Instantly share code, notes, and snippets.

@jeffsheets
jeffsheets / RestResponseEntityExceptionHandler.java
Last active October 31, 2020 11:44
Handle REST Exceptions in Spring
/**
* REST exception handlers defined at a global level for the application
*/
@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(RestResponseEntityExceptionHandler.class);
/**
* Catch all for any other exceptions...
*/
@jeffsheets
jeffsheets / Jenkinsfile
Created December 8, 2020 22:31
Minimal Gradle Jenkinsfile
//Inspired by https://gist.github.com/aerobless/37561bb0fb45b7e8732beaafad1cba26
pipeline {
agent any
triggers {
//Needed by Bitbucket to see the builds on PRs https://stackoverflow.com/a/54710254
pollSCM('')
}
stages {
@jeffsheets
jeffsheets / LocalDataSetup.java
Last active December 31, 2020 14:50
Spring Boot Initialize Local Test Data on Startup
/** Shows how to use code to init data instead of using data.sql or import.sql with Spring Boot */
@Component
@Profile({"local", "test"})
@RequiredArgsConstructor //Lombok magic to autowire the final fields (optional)
public class LocalDataSetup implements ApplicationRunner {
private final MyObjectRepository myObjectRepository;
@Override
public void run(ApplicationArguments args) {
@jeffsheets
jeffsheets / 1-websyncConfig.js
Last active January 25, 2021 22:42
AWS S3 Deploy sync w/ Cloudfront Invalidation and Cache-Control headers for Create-React-App
//Config for s3 websync with cloudfront invalidation
// and Cache-Control headers for a Create-React-App application
//https://www.npmjs.com/package/websync
module.exports = {
source: "build/",
target: `s3://yourcompany-us-east-1-${process.env.CI_ENVIRONMENT_NAME}-createreactapp/`,
invalidateDeletes: true,
modifiers: {
"static/**": { CacheControl: "max-age=31536000" },
@jeffsheets
jeffsheets / ColumnRowMapper.java
Last active January 25, 2021 23:36
Use Spring JdbcTemplate to easily read data, without Spring Data JPA, and map columns using Column DTO mappings, using a secondary database connection
/** Based nearly completely on this same code from https://stackoverflow.com/a/52534584/1469525 */
@Slf4j
public class ColumnRowMapper<T> extends BeanPropertyRowMapper<T> {
private ColumnRowMapper(final Class<T> mappedClass) { super(mappedClass); }
@Override
protected String underscoreName(final String name) {
final Column annotation;
final String columnName;
@jeffsheets
jeffsheets / PapioMenuAlexaAppPrivacyPolicy.txt
Last active February 19, 2021 19:55
Papio Menu Alexa App Privacy Policy
The Papio Menu Alexa app available on Amazon Alexa does NOT collect any information.
The app is free for anyone to use on Alexa
@jeffsheets
jeffsheets / ClientJWTService.groovy
Last active March 24, 2021 02:00
JWT creation in Spring Boot Groovy or Java with RSA 512 or 256 algorithm including steps to generate the keys stored as strings in yml properties file
@Service
class ClientJWTService {
@Value('${client.publicKey}')
String publicKeyString
@Value('${client.privateKey}')
String privateKeyString
Algorithm buildJwtAlgorithm() {
KeyFactory kf = KeyFactory.getInstance('RSA')
@jeffsheets
jeffsheets / EmbeddedMariaDbConfig.groovy
Last active April 26, 2021 12:31
Embedded MariaDB4j Spring Boot Configuration
import ch.vorburger.mariadb4j.DBConfigurationBuilder
import ch.vorburger.mariadb4j.springframework.MariaDB4jSpringService
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import javax.sql.DataSource
@jeffsheets
jeffsheets / timeZoneUtils.js
Last active November 5, 2021 02:49
Browser Javascript to get user timezone or offset that is IE11 safe when passed to a Java API, with Jest Test blogpost: https://www.sheetsj.com/2021/05/mock-intl-and-date-globals-in-jest.html
/**
* Useful when passing the browser timezone to a backend Java API that reads a timezone in using ZoneId.of(tz),
* as both 'America/Chicago' and '-0600' are valid values when passed to the Java API.
* The Offset is used to handle IE11 and other older browsers.
*/
export const getUserTimeZoneOrOffset = () => {
let timeZone;
try {
timeZone = new Intl.DateTimeFormat().resolvedOptions().timeZone;
} catch (error) {
@jeffsheets
jeffsheets / HibernatePersistenceProviderResolver.java
Created May 20, 2016 19:51
Use JPA 2.1 and Hibernate 4.3.11 on Websphere 8.5.5.x
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceProviderResolver;
import javax.persistence.spi.PersistenceProviderResolverHolder;
import java.util.Collections;
import java.util.List;