Spring Boot with all examples

If you want to Spring Boot that has REST, JSP, Filter, DB template with JDBI, DB Transaction control, auto update changes.

This page is for you.

Full Code with Spring Boot 1.5.8.RELEASE:

0_spring-boot-jdbi-all.zip

What you need for run this:

  • Maven 3
  • Java 8
  • MySQL 5.x

What the example will support (Click to see more detail):

Coming Soon:

  • Support Unit Test with Spring Boot
  • Support Integration Test with Spring Boot
  • More example about DB transaction with Spring Boot

Important thing you must know:

        <!-- to deploy as a war in tomcat -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

Note that if you deploy as war, these 2 properties in the application. properties will be useless:

server.contextPath=/test
server.port=8080

So, the context path and port will depend on your Tomcat configuration.

How to run it:

create a database named “testdb” and run dbscript.sql to the table in MySql:

CREATE TABLE users (
  id               INT PRIMARY KEY AUTO_INCREMENT,
  username             VARCHAR(100) NOT NULL
);

change application.properties to your MySql username and password:

#Database
spring.datasource.url=jdbc:mysql://localhost/testdb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

run this in console:

mvn clean package

Then run this to start:

mvn clean spring-boot:run

You should see something like this:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.8.RELEASE)

.....
.....
2017-12-12 22:03:04.243  INFO 260 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.Annotation
ConfigEmbeddedWebApplicationContext@7595f8d7: startup date [Tue Dec 12 22:03:01 CET 2017]; root of context hierarchy

Open a browswer and try to go to this url:

http://localhost:8080/test/api/user/list

Note that the the context path is “/test”.

You should see this:

This is what you can do:

you can try to access these URLs
   To create new row in database
        http://localhost:8080/test/api/user/insertOK
   To create new row in database and throw exception (it will rollback)
        http://localhost:8080/test/api/user/insertNotOK
   To see rows from database
        http://localhost:8080/test/api/user/list
   To test filter:
        http://localhost:8080/test/api/count
        http://localhost:8080/test/api/count2
you can try to edit webapp/static/test-static.txt and go to  http://localhost:8080/test/static/test-static.txt
you can try to edit webapp/WEB-INFO/jsp/testjsp-view.jsp and go to  http://localhost:8080/test/testjsp

See the link above to see the detail which configurations do that for you.