This is a tutorial on how to use Docker (Dockerize) a Spring boot application using Maven Follow the official spring tutorial here On the root folder create a new folder with a Dockerfile in it: mkdir docker && touch docker/Dockerfile FROM openjdk:8-jdk EXPOSE 8080 COPY dockerizing-springboot-0.1.0.jar /opt/boot.jar ENTRYPOINT java -jar /opt/boot.jar Add the following lines to pom.xml (inside build -> plugins tag) <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <configuration> <imageName>springbootsample</imageName> <dockerDirectory>docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> Run the command to build a new image (called springbootsample): mvn clean install docker:build Start the container with the following command: docker run...
Posted on May 22, 2018
This post has the intention of exploring new Java 8 features like Lambda. Lambda Expressions Is a feature already present in other languages, and this enables us to write more readable code in Java. Grants us the hability to express more with less lines of code. Variables in Lambda An important thing to say is that variables used in lambda should be final. This means for example that the following is NOT allowed: Integer mySum =0; numbers.forEach( i -> mySum = mySum+ i); //Not allowed! So in practice, we are not allowed to change the object, but we can change...
Posted on Jun 6, 2017
Hereby a few examples about how to use stream + filter to get all elements matching some kind of conditions. Example 1 - Integer array List<Integer> listTest = DataFactory.createListInteger(10); List<Integer> returnedList = listTest.stream().filter(i -> i % 2 ==0).collect(Collectors.toList()); assertTrue(returnedList.size() > 0); assertTrue(returnedList.size() != 10); assertTrue(returnedList.get(0) == 0); assertTrue(returnedList.get(1) == 2); assertTrue(returnedList.get(2) == 4); Example 2 - get all ids from object package lab.j8; public class Customer { private Integer id; private String name; public Customer(Integer id, String name) { this.id = id; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id =...
Posted on Jun 5, 2017
The exception This exception happens when there is one physical table mapping two or more logical table names. This happened to me because there are multiple @JoinColumns using the same column name (user_id) How to fix Set a different name for each one of the JoinColumns (defined in the name parameter in the annotation): @OneToOne @JoinColumn(name = "local_user_id") public User getUser() { return user; } @OneToOne @JoinColumns({@JoinColumn(name = "userId", referencedColumnName = "userId"), @JoinColumn(name = "providerId", referencedColumnName = "providerId"), @JoinColumn(name = "providerUserId", referencedColumnName = "providerUserId")} ) public UserConnection getUserConnection() { return userConnection; } Reference error org.hibernate.DuplicateMappingException: Table [profile] contains physical column...
Posted on Nov 14, 2016
Appending is a really common operation when programming, but how do we append two arrays / slices in Golang? Well, first lets try it: myStringSlice := []string{"first", "second", "third"} myStringSlice = append(myStringSlice, []string{"fourth", "fift"}) But this error pops up: cannot use []string literal (type []string) as type string in append Then how do we do this? We could iterate with a for but it doesn’t look nice. The alternative option to append on arrays / slices in GoLang is to add the “…” syntax after the array / slice: myStringSlice := []string{"first", "second", "third"} //ERROR! //myStringSlice = append(myStringSlice, []string{"fourth", "fift"})...
Posted on Nov 9, 2016
Lately I have been studying JPA / Hibernate on Spring boot, and I got a strange exception on startup (on gradle bootRun): org.hibernate.MappingException. Problem ocurred because I had mixed annotations on private attributes and public getters. How to fix: Put all annotations on public getters instead of private attribute declaration or just put all annotations on private attributes and remove from public getters. @OneToOne @JoinColumn(name = "local_user_id") public User getUser() { return user; } @OneToOne @JoinColumns({@JoinColumn(name = "userId", referencedColumnName = "userId"), @JoinColumn(name = "providerId", referencedColumnName = "providerId"), @JoinColumn(name = "providerUserId", referencedColumnName = "providerUserId")} ) public UserConnection getUserConnection() { return userConnection;...
Posted on Nov 5, 2016
One of the worst pains in Java is having to stop / start a server at each simple change in the code (reload it). Pretty much all IDEs provide support for hot code swapping with A LOT of limitations. To do hot code swapping you must enable debug and then connect from the IDE to the given port: -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044 But what if we want to make more drastic changes in the code? Well, there is JRebel (a paid solution) and spring loaded (free). So I will cover the free tool =) So how do we set this up in...
Posted on Nov 2, 2016
This post will cover a small sample about how to capture or recover from panic in Golang. Lets check this scenario: var secondVar interface{} secondVar = 10 //.... //some code secondString = secondVar.(string) What happens in this case? panic: interface conversion: interface is int, not string And when this happens, your program crashes and shuts down. And this can be really bad depending on the situation. Golang has a recover command / function that allows you to capture this behaviour using a defer: defer func() { if r := recover(); r != nil { fmt.Println("Recovered in f", r) } }()...
Posted on Oct 23, 2016
Lets say you have an interface in Golang, and for some reason you want to cast it. How do we do that? var myVar interface{} ... myVar = secondVar.(string) But what happens when myVar is not a string? Well, this happens: panic: interface conversion: interface is int, not string The program does a panic and the whole executable exits and shutdown. Luckily, there is a safe / safer way to avoid system panic when casting in GoLang: var firstVar interface{} firstVar = "this is a string" firstString, ok := firstVar.(string) if (!ok) { fmt.Printf("firstString is not a string, do something...
Posted on Oct 22, 2016
This post contains samples and considerations about the basics of Java Threads, excluding the executors framework (executors framework will be in a future post). The code will cover the two ways to create and run threads and a small test on how to start them. Extend Thread The first way to create a thread is extending the Thread class and overriding the run method: package com.thread; public class SayHelloExtend extends Thread { @Override public void run(){ System.out.println("I am extending a thread!"); } } Implement Runnable The second way to create e thread is to implement the Runnable interface. This is...