Simple web-app example for spring-boot-data-jdbc
authorKai Moritz <kai@juplo.de>
Fri, 10 Jul 2020 12:54:21 +0000 (14:54 +0200)
committerKai Moritz <kai@juplo.de>
Mon, 14 Sep 2020 20:02:22 +0000 (22:02 +0200)
.gitignore [new file with mode: 0644]
pom.xml [new file with mode: 0644]
src/main/java/de/juplo/boot/data/jdbc/Application.java [new file with mode: 0644]
src/main/java/de/juplo/boot/data/jdbc/User.java [new file with mode: 0644]
src/main/java/de/juplo/boot/data/jdbc/UserController.java [new file with mode: 0644]
src/main/java/de/juplo/boot/data/jdbc/UserRepository.java [new file with mode: 0644]
src/main/resources/application.properties [new file with mode: 0644]
src/main/resources/schema.sql [new file with mode: 0644]
src/test/java/de/juplo/boot/data/jdbc/ApplicationTests.java [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..f686482
--- /dev/null
@@ -0,0 +1,3 @@
+target
+.idea
+outbox.iml
diff --git a/pom.xml b/pom.xml
new file mode 100644 (file)
index 0000000..10293a1
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,82 @@
+<?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>
+   <parent>
+     <groupId>org.springframework.boot</groupId>
+     <artifactId>spring-boot-starter-parent</artifactId>
+     <version>2.1.5.RELEASE</version>
+     <relativePath/> <!-- lookup parent from repository -->
+   </parent>
+   <groupId>de.juplo.boot.data</groupId>
+   <artifactId>jdbc</artifactId>
+   <version>0.0.1-SNAPSHOT</version>
+   <name>data-jdbc</name>
+   <description>Simple web-app example for spring-boot-data-jdbc</description>
+
+   <properties>
+     <java.version>1.8</java.version>
+   </properties>
+
+   <dependencies>
+     <dependency>
+       <groupId>org.springframework.boot</groupId>
+       <artifactId>spring-boot-starter-web</artifactId>
+     </dependency>
+     <dependency>
+       <groupId>org.springframework.boot</groupId>
+       <artifactId>spring-boot-starter-actuator</artifactId>
+     </dependency>
+     <dependency>
+       <groupId>org.springframework.boot</groupId>
+       <artifactId>spring-boot-starter-data-jdbc</artifactId>
+     </dependency>
+     <dependency>
+       <groupId>org.springframework.boot</groupId>
+       <artifactId>spring-boot-starter-json</artifactId>
+     </dependency>
+     <dependency>
+       <groupId>org.projectlombok</groupId>
+       <artifactId>lombok</artifactId>
+     </dependency>
+
+     <dependency>
+       <groupId>com.h2database</groupId>
+       <artifactId>h2</artifactId>
+       <scope>runtime</scope>
+     </dependency>
+
+     <dependency>
+       <groupId>org.springframework.boot</groupId>
+       <artifactId>spring-boot-starter-test</artifactId>
+       <scope>test</scope>
+     </dependency>
+   </dependencies>
+
+   <build>
+     <plugins>
+       <plugin>
+         <groupId>io.fabric8</groupId>
+         <artifactId>docker-maven-plugin</artifactId>
+         <version>0.33.0</version>
+         <configuration>
+           <images>
+             <image>
+               <name>%a:%l</name>
+             </image>
+           </images>
+         </configuration>
+         <executions>
+           <execution>
+              <id>build</id>
+              <phase>package</phase>
+              <goals>
+                <goal>build</goal>
+              </goals>
+           </execution>
+         </executions>
+       </plugin>
+     </plugins>
+   </build>
+
+</project>
diff --git a/src/main/java/de/juplo/boot/data/jdbc/Application.java b/src/main/java/de/juplo/boot/data/jdbc/Application.java
new file mode 100644 (file)
index 0000000..d7301e1
--- /dev/null
@@ -0,0 +1,17 @@
+package de.juplo.boot.data.jdbc;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class Application {
+
+    private final static Logger LOG = LoggerFactory.getLogger(Application.class);
+
+
+    public static void main(String[] args) {
+        SpringApplication.run(Application.class, args);
+    }
+}
diff --git a/src/main/java/de/juplo/boot/data/jdbc/User.java b/src/main/java/de/juplo/boot/data/jdbc/User.java
new file mode 100644 (file)
index 0000000..6d4d552
--- /dev/null
@@ -0,0 +1,26 @@
+package de.juplo.boot.data.jdbc;
+
+import lombok.*;
+import org.springframework.data.annotation.Id;
+
+import java.time.LocalDateTime;
+
+public class User {
+    @Id
+    Long id;
+    @Getter
+    @Setter
+    String username;
+    @Getter
+    @Setter
+    LocalDateTime created;
+    @Getter
+    @Setter
+    boolean loggedIn;
+
+    public User(String username, LocalDateTime created, boolean loggedIn) {
+        this.username = username;
+        this.created = created;
+        this.loggedIn = loggedIn;
+    }
+}
diff --git a/src/main/java/de/juplo/boot/data/jdbc/UserController.java b/src/main/java/de/juplo/boot/data/jdbc/UserController.java
new file mode 100644 (file)
index 0000000..4eb1094
--- /dev/null
@@ -0,0 +1,78 @@
+package de.juplo.boot.data.jdbc;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.ResponseEntity;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
+import org.springframework.web.util.UriComponents;
+
+import java.time.LocalDateTime;
+
+@RestController
+@Transactional
+@RequestMapping("/users")
+public class UserController {
+
+    private static final Logger LOG = LoggerFactory.getLogger(UserController.class);
+
+
+    private final UserRepository repository;
+
+
+    public UserController(UserRepository repository) {
+        this.repository = repository;
+    }
+
+
+    @PostMapping
+    public ResponseEntity<Void> createUser(
+            ServletUriComponentsBuilder builder,
+            @RequestBody String username) {
+        String sanitizedUsername = UserController.sanitize(username);
+        User user = new User(sanitizedUsername, LocalDateTime.now(), false);
+        repository.save(user);
+        UriComponents uri =
+            builder
+                .fromCurrentRequest()
+                .pathSegment("{username}")
+                .buildAndExpand(sanitizedUsername);
+        return ResponseEntity.created(uri.toUri()).build();
+    }
+
+    @GetMapping("{username}")
+    public ResponseEntity<User> getUser(@PathVariable String username) {
+        User user = repository.findByUsername(UserController.sanitize(username));
+
+        if (user == null)
+            return ResponseEntity.notFound().build();
+
+        return ResponseEntity.ok(user);
+    }
+
+    @DeleteMapping("{username}")
+    public ResponseEntity<User> removeUser(@PathVariable String username) {
+        User user = repository.findByUsername(UserController.sanitize(username));
+
+        if (user == null)
+            return ResponseEntity.notFound().build();
+
+        repository.delete(user);
+
+        return ResponseEntity.ok(user);
+    }
+
+    @GetMapping()
+    public ResponseEntity<Iterable<User>> getUsers() {
+        return ResponseEntity.ok(repository.findAll());
+    }
+
+
+    private static String sanitize(String string) {
+        if (string == null)
+            return "";
+
+        return string.trim().toLowerCase();
+    }
+}
diff --git a/src/main/java/de/juplo/boot/data/jdbc/UserRepository.java b/src/main/java/de/juplo/boot/data/jdbc/UserRepository.java
new file mode 100644 (file)
index 0000000..1d07359
--- /dev/null
@@ -0,0 +1,10 @@
+package de.juplo.boot.data.jdbc;
+
+import org.springframework.data.jdbc.repository.query.Query;
+import org.springframework.data.repository.CrudRepository;
+import org.springframework.data.repository.query.Param;
+
+public interface UserRepository extends CrudRepository<User, Long> {
+    @Query("select * from User u where u.username = :username")
+    User findByUsername(@Param("username") String username);
+}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
new file mode 100644 (file)
index 0000000..821da09
--- /dev/null
@@ -0,0 +1 @@
+management.endpoints.web.exposure.include=*
diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql
new file mode 100644 (file)
index 0000000..353c36b
--- /dev/null
@@ -0,0 +1 @@
+CREATE TABLE user(id BIGINT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(255), created TIMESTAMP, logged_in BIT)
diff --git a/src/test/java/de/juplo/boot/data/jdbc/ApplicationTests.java b/src/test/java/de/juplo/boot/data/jdbc/ApplicationTests.java
new file mode 100644 (file)
index 0000000..c77d5b7
--- /dev/null
@@ -0,0 +1,16 @@
+package de.juplo.boot.data.jdbc;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class ApplicationTests {
+
+    @Test
+    public void contextLoads() {
+    }
+
+}