From 355931f8260616faf25ef79f2c282d84fddc5733 Mon Sep 17 00:00:00 2001 From: eugenp Date: Fri, 24 Feb 2017 15:12:23 +0200 Subject: [PATCH] adding some simple persistence --- spring-5/pom.xml | 200 +++++++++--------- .../java/com/baeldung/Spring5Application.java | 1 + .../baeldung/persistence/DataSetupBean.java | 26 +++ .../baeldung/persistence/FooRepository.java | 10 + .../src/main/java/com/baeldung/web/Foo.java | 84 ++++++++ .../java/com/baeldung/web/FooController.java | 62 ++++++ .../src/main/resources/application.properties | 5 +- 7 files changed, 291 insertions(+), 97 deletions(-) create mode 100644 spring-5/src/main/java/com/baeldung/persistence/DataSetupBean.java create mode 100644 spring-5/src/main/java/com/baeldung/persistence/FooRepository.java create mode 100644 spring-5/src/main/java/com/baeldung/web/Foo.java create mode 100644 spring-5/src/main/java/com/baeldung/web/FooController.java diff --git a/spring-5/pom.xml b/spring-5/pom.xml index 3b1d6c712b..ab05918ae4 100644 --- a/spring-5/pom.xml +++ b/spring-5/pom.xml @@ -1,108 +1,116 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - com.baeldung - spring-5 - 0.0.1-SNAPSHOT - jar + com.baeldung + spring-5 + 0.0.1-SNAPSHOT + jar - spring-5 - + spring-5 + - - org.springframework.boot - spring-boot-starter-parent - 2.0.0.BUILD-SNAPSHOT - - + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.BUILD-SNAPSHOT + + - - UTF-8 - UTF-8 - 1.8 - + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-validation + + + org.springframework.boot + spring-boot-starter-web + - - - org.springframework.boot - spring-boot-starter-data-jpa - - - org.springframework.boot - spring-boot-starter-security - - - org.springframework.boot - spring-boot-starter-validation - - - org.springframework.boot - spring-boot-starter-web - + + + org.apache.commons + commons-lang3 + - - org.springframework.boot - spring-boot-devtools - runtime - - - com.h2database - h2 - runtime - - - org.springframework.boot - spring-boot-starter-test - test - - + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + org.springframework.boot + spring-boot-devtools + runtime + + + com.h2database + h2 + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + UTF-8 + UTF-8 + 1.8 + diff --git a/spring-5/src/main/java/com/baeldung/Spring5Application.java b/spring-5/src/main/java/com/baeldung/Spring5Application.java index 4f5d431b0f..41b5c1eed1 100644 --- a/spring-5/src/main/java/com/baeldung/Spring5Application.java +++ b/spring-5/src/main/java/com/baeldung/Spring5Application.java @@ -9,4 +9,5 @@ public class Spring5Application { public static void main(String[] args) { SpringApplication.run(Spring5Application.class, args); } + } diff --git a/spring-5/src/main/java/com/baeldung/persistence/DataSetupBean.java b/spring-5/src/main/java/com/baeldung/persistence/DataSetupBean.java new file mode 100644 index 0000000000..7936a2b7af --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/persistence/DataSetupBean.java @@ -0,0 +1,26 @@ +package com.baeldung.persistence; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; + +import java.util.stream.IntStream; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.baeldung.web.Foo; + +@Component +public class DataSetupBean implements InitializingBean { + + @Autowired + private FooRepository repo; + + // + + @Override + public void afterPropertiesSet() throws Exception { + IntStream.range(1, 20).forEach(i -> repo.save(new Foo(randomAlphabetic(8)))); + } + +} diff --git a/spring-5/src/main/java/com/baeldung/persistence/FooRepository.java b/spring-5/src/main/java/com/baeldung/persistence/FooRepository.java new file mode 100644 index 0000000000..1f1e071158 --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/persistence/FooRepository.java @@ -0,0 +1,10 @@ +package com.baeldung.persistence; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +import com.baeldung.web.Foo; + +public interface FooRepository extends JpaRepository, JpaSpecificationExecutor { + +} diff --git a/spring-5/src/main/java/com/baeldung/web/Foo.java b/spring-5/src/main/java/com/baeldung/web/Foo.java new file mode 100644 index 0000000000..c4868a9958 --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/web/Foo.java @@ -0,0 +1,84 @@ +package com.baeldung.web; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Foo { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + private String name; + + public Foo() { + super(); + } + + public Foo(final String name) { + super(); + + this.name = name; + } + + public Foo(final long id, final String name) { + super(); + + this.id = id; + this.name = name; + } + + // API + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + // + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Foo other = (Foo) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + + @Override + public String toString() { + return "Foo [name=" + name + "]"; + } + +} \ No newline at end of file diff --git a/spring-5/src/main/java/com/baeldung/web/FooController.java b/spring-5/src/main/java/com/baeldung/web/FooController.java new file mode 100644 index 0000000000..de6928033e --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/web/FooController.java @@ -0,0 +1,62 @@ +package com.baeldung.web; + +import java.util.List; + +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.http.HttpStatus; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.persistence.FooRepository; + +@RestController("/foos") +public class FooController { + + @Autowired + private FooRepository repo; + + // API - read + + @RequestMapping(method = RequestMethod.GET, value = "/foos/{id}") + @ResponseBody + @Validated + public Foo findById(@PathVariable @Min(0) final long id) { + return repo.findOne(id); + } + + @RequestMapping(method = RequestMethod.GET) + @ResponseBody + public List findAll() { + return repo.findAll(); + } + + @RequestMapping(params = { "page", "size" }, method = RequestMethod.GET) + @ResponseBody + @Validated + public List findPaginated(@RequestParam("page") @Min(0) final int page, @Max(100) @RequestParam("size") final int size) { + final Page resultPage = repo.findAll(new PageRequest(page, size)); + return resultPage.getContent(); + } + + // API - write + + @RequestMapping(method = RequestMethod.PUT, value = "/foos/{id}") + @ResponseStatus(HttpStatus.OK) + @ResponseBody + public Foo update(@PathVariable("id") final String id, @RequestBody final Foo foo) { + return foo; + } + +} diff --git a/spring-5/src/main/resources/application.properties b/spring-5/src/main/resources/application.properties index bafddced85..2e33b98523 100644 --- a/spring-5/src/main/resources/application.properties +++ b/spring-5/src/main/resources/application.properties @@ -1 +1,4 @@ -server.port=8081 \ No newline at end of file +server.port=8081 + +security.user.name=user +security.user.password=pass \ No newline at end of file