DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Web App Development with SpringBoot


HelloWorld

Install SDKMAN, JDK and Spring:

curl -s "https://get.sdkman.io" | bash
sdk install java
sdk install springboot
cat << EOF > app.groovy
@RestController
class ThisWillActuallyRun {

    @RequestMapping("/")
    String home() {
        "Hello World!"
    }

}
EOF
spring run app.groovy
firefox localhost:8080

If port 8080 has been used by other Tomcat instance, use spring run app.groovy -- --server.port=9000 to use another port. See 59. Using the CLI for details. It takes less than 1 minute to start web server on linode VPS (outside GFW), or on my laptop in the morning. But run the same app in the afternoon, it hangs up.

Downloading is also very fast (less than 2 minutes) on linode:

$ leo@linode:~$ du -sh .sdkman/
721M    .sdkman/

But extremely slow on laptop.

Package it as a standalone jar file (with a tomcat server inside):

spring jar app.jar app.groovy
java -jar app.jar

Full Project

Create project scaffold with spring init -d=web,jpa --build=gradle --package-name='com.dh' myJavaProject in Java, or spring init -d=web,jpa --build=gradle --package-name='com.dh' -l groovy myGroovyPrj in Groovy.

Fix error "Cannot determine embedded database driver class for database type NONE": add compile('com.h2database:h2') into $PROJ_HOME/build.gradle, run gradle bootRun.

Auto restart when files changed

Tow steps:

  1. Add compile("org.springframework.boot:spring-boot-devtools") into section dependencies of build.gradle, this makes the server restart when java class file changed. See 20. Developer tools for details;

  2. If you use IDEA, press Ctrl-Shift-A and input 'build project', check "Build project automatically" in Settings. This make IDEA rebuild project after you save the file.

Note: this doesn't work in Eclipse. Don't konw why.

STS (eclipse)

Add apply plugin: 'eclipse' into build.gradle, create eclipse file: gradle eclipse. Start STS(Spring Tool Suite), import the project into Eclipse: File -> Import... -> Existing Projects into Workspace. Use gradle cleanEclipse to clean eclipse files. Use gradle tasks to list all available tasks.

IntelliJ IDEA

Add apply plugin: 'idea' into build.gradle, run gradle idea. Start IDEA, open the project as a Gradle project into IntelliJ. IDEA will download all jars specified in gradle automatically. If downloading is slow, you can run it at morning, or adding a proxy. Use gradle cleanIdea to clean IDEA files.

Database Management

H2 database

Visit database with browser: URL: http://localhost:8080/h2-console/, JDBC URL: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1, User Name: sa. Other items leave untouched, and click Connect button.

You can use multiple connections at the same time. Get the default H2 JDBC URL from post View content of embedded H2 database started by Spring.

I tried to connect to the H2 database using SQuirreL SQL Client with driver H2, H2 Embedded and H2 In-Memory, but all failed.

Configuration

In file $PROJ_HOME/src/main/resources/application.propertiesh:

server.port = 9000

Ref:

10. Installing Spring Boot



Published

Dec 14, 2016

Last Updated

Dec 14, 2016

Category

Tech

Tags

  • sdkman 3
  • spring boot 1

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor