Skip to content

Instantly share code, notes, and snippets.

If not already added, add remote to original project: git remote add upstream https://github.com/spring-projects/spring-boot
Fetch original project:
git fetch upstream
Rebase the master branch of original project:
git co master
git rebase upstream/master
git push -f origin master
@JorgenRingen
JorgenRingen / ConfiguringHibernatewithSpring.java
Created January 10, 2018 08:58
How to bootstrap hibernate with Spring
@Configuration
@EnableTransactionManagement
public class HibernateConf {
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(
{"org.example.model" });
@JorgenRingen
JorgenRingen / jenkins_avoid_build_loop_on_tag.groovy
Created January 18, 2018 08:05
How to avoid build loop when jenkins pushes a tag (jenkinsfile)
stage ('Publish version') {
def commitAuthor = sh(script: "git --no-pager show -s --format='%an'", returnStdout: true).trim()
def branchIsDevelop = "develop".equals(env.BRANCH_NAME);
def authorIsJenkins = "TeamJenkins".equals(commitAuthor)
if(branchIsDevelop && !authorIsJenkins) {
echo "Dette er et nytt bygg på develop. Bumper versjon og publiserer!"
sh "git checkout ${env.BRANCH_NAME}"
@JorgenRingen
JorgenRingen / Java8ChainedComparatorTest.java
Created February 6, 2018 18:54
Testing chained comparators from java 8 (about 10% decrease in performance compared to pre-java8 implementation)
public class Java8ComparatorTest {
@Test
public void testJava8ChainedComparators() {
Comparator<Person> personComparator = Comparator.comparingInt(Person::getAge)
.thenComparing(Person::getName)
.thenComparing(Person::getOccupation, (o1, o2) -> {
return o1.compareTo(o2);
});
@JorgenRingen
JorgenRingen / WrappedresourcesspringhateoasApplication.java
Created March 8, 2018 11:59
Using Resources.wrap from spring hateoas
@SpringBootApplication
public class WrappedresourcesspringhateoasApplication {
public static void main(String[] args) {
SpringApplication.run(WrappedresourcesspringhateoasApplication.class, args);
}
}
@RestController
class FooController {
@JorgenRingen
JorgenRingen / Customized http command
Last active March 15, 2018 12:31
Example of customized httpie launcher that set headers
https://httpie.org/
File that needs to be available and executable on path:
#!/bin/bash
USERID="user-id:AKRF1110"
XAUTHTOKEN="X-Auth-Token:$(cat /tmp/jwttoken.txt)"
http --timeout=9999999 "$@" $USERID $XAUTHTOKEN
Test:
@JorgenRingen
JorgenRingen / GenericErrorHandlingWithSupplier.java
Created March 22, 2018 08:41
Do generic error-handling by wrapping method calls in a Supplier
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class GenericErrorHandlingWithSupplier {
private static final Logger LOGGER = LoggerFactory.getLogger(GenericErrorHandlingWithSupplier.class);
public static void main(String[] args) {
// Example 1: Every caller needs to catch all relevant exceptions
@JorgenRingen
JorgenRingen / pom.xml
Created April 5, 2018 10:21
Merging keys with yaml
<?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>
<groupId>com.example</groupId>
<artifactId>yaml-merge-keys</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
@JorgenRingen
JorgenRingen / SpringProfilesTest.java
Last active April 10, 2018 09:09
Test configuration for different spring profiles
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Testfile located in src/test/resources/testdata/my-test-data.xml.
src/test/resources is current directory when running the test, so not needed in path.
new String(Files.readAllBytes(Paths.get(this.getClass().getResource("/testdata/my-test-data.xml").getPath())))