Skip to content

Commit c8e9550

Browse files
committed
add project for test update api with spring boot
1 parent 8aab428 commit c8e9550

32 files changed

+1201
-0
lines changed

rest-jpa-update/.gitignore

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/src/main/resources/application.properties
2+
/src/test/resources/application.properties
3+
/target
4+
/node_modules
5+
.gradle
6+
/build/
7+
/yarn-error.log
8+
9+
# Ignore Gradle GUI config
10+
gradle-app.setting
11+
12+
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
13+
!gradle-wrapper.jar
14+
15+
# Cache of project
16+
.gradletasknamecache
17+
18+
# User-specific stuff:
19+
.idea/**/workspace.xml
20+
.idea/**/tasks.xml
21+
.idea/dictionaries
22+
.idea/artifacts
23+
24+
# Sensitive or high-churn files:
25+
.idea/**/dataSources/
26+
.idea/**/dataSources.ids
27+
.idea/**/dataSources.xml
28+
.idea/**/dataSources.local.xml
29+
.idea/**/sqlDataSources.xml
30+
.idea/**/dynamic.xml
31+
.idea/**/uiDesigner.xml
32+
.idea/inspectionProfiles
33+
34+
# Gradle:
35+
.idea/**/gradle.xml
36+
.idea/**/libraries
37+
38+
# CMake
39+
cmake-build-debug/
40+
41+
# Mongo Explorer plugin:
42+
.idea/**/mongoSettings.xml
43+
44+
## File-based project format:
45+
*.iws
46+
47+
## Plugin-specific files:
48+
49+
# IntelliJ
50+
/out/
51+
52+
# mpeltonen/sbt-idea plugin
53+
.idea_modules/
54+
55+
# JIRA plugin
56+
atlassian-ide-plugin.xml
57+
58+
# Cursive Clojure plugin
59+
.idea/replstate.xml
60+
61+
# Ruby plugin and RubyMine
62+
/.rakeTasks
63+
64+
# Crashlytics plugin (for Android Studio and IntelliJ)
65+
com_crashlytics_export_strings.xml
66+
crashlytics.properties
67+
crashlytics-build.properties
68+
fabric.properties
69+
70+
### JetBrains Patch ###
71+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
72+
73+
# *.iml
74+
# modules.xml
75+
.idea/misc.xml
76+
# *.ipr
77+
78+
# Sonarlint plugin
79+
.idea/sonarlint
80+
/.idea/.name
81+
/config.properties
82+
/src/main/webapp/manifest.json
83+
/src/main/resources/db.properties
84+
/src/test/resources/db-test.properties
85+
/.idea/hotswap_agent.xml

rest-jpa-update/.idea/codeStyles/codeStyleConfig.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rest-jpa-update/.idea/compiler.xml

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rest-jpa-update/.idea/encodings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rest-jpa-update/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rest-jpa-update/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rest-jpa-update/pom.xml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.concretepage</groupId>
7+
<artifactId>rest-jpa-update</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
<name>spring-demo</name>
11+
<description>Spring Boot Demo Project</description>
12+
13+
<parent>
14+
<groupId>org.springframework.boot</groupId>
15+
<artifactId>spring-boot-starter-parent</artifactId>
16+
<version>2.1.2.RELEASE</version>
17+
</parent>
18+
<properties>
19+
<java.version>1.8</java.version>
20+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-data-jpa</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-devtools</artifactId>
33+
<optional>true</optional>
34+
</dependency>
35+
<dependency>
36+
<groupId>mysql</groupId>
37+
<artifactId>mysql-connector-java</artifactId>
38+
<scope>runtime</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.h2database</groupId>
42+
<artifactId>h2</artifactId>
43+
<scope>runtime</scope>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-hateoas</artifactId>
49+
</dependency>
50+
51+
<!-- Testing -->
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-test</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>com.github.springtestdbunit</groupId>
59+
<artifactId>spring-test-dbunit</artifactId>
60+
<version>1.3.0</version>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.dbunit</groupId>
65+
<artifactId>dbunit</artifactId>
66+
<version>2.5.4</version>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>com.jcabi</groupId>
71+
<artifactId>jcabi-matchers</artifactId>
72+
<version>1.3</version>
73+
<scope>test</scope>
74+
</dependency>
75+
76+
</dependencies>
77+
<build>
78+
<plugins>
79+
<plugin>
80+
<groupId>org.springframework.boot</groupId>
81+
<artifactId>spring-boot-maven-plugin</artifactId>
82+
</plugin>
83+
</plugins>
84+
</build>
85+
</project>

rest-jpa-update/readme.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Example jpa rest with update problems
2+
3+
## Configuration
4+
5+
For main and test dirs:
6+
`cp application.properties.dist application.properties`
7+
8+
# Examples of implementations update
9+
10+
## Classic version
11+
12+
See test: `/src/test/java/hello/controller/classic/EmployeeControllerTest.java`
13+
14+
In controller:
15+
```
16+
@PatchMapping("/{id}")
17+
public Employee update(@RequestBody Employee employee, @PathVariable Long id) {
18+
return service.update(id, employee);
19+
}
20+
```
21+
22+
In service:
23+
```
24+
public T update(ID id, T entity) {
25+
Optional<T> optionalEntityFromDB = getRepository().findById(id);
26+
return optionalEntityFromDB
27+
.map(e -> saveAndReturnSavedEntity(entity, e))
28+
.orElseThrow(getNotFoundExceptionSupplier("Cannot update - not exist entity by id: " + id, OBJECT_NOT_FOUND));
29+
}
30+
31+
private T saveAndReturnSavedEntity(T entity, T entityFromDB) {
32+
entity.setId(entityFromDB.getId());
33+
return getRepository().save(entity);
34+
}
35+
```
36+
37+
Pros:
38+
* simple implement
39+
* auto deserialization to entity in controller
40+
* enable add Bean Validation from the box (@Valid @RequestBody Employee employee)
41+
42+
Cons:
43+
* cannot detect passed from json null field or not passed nothing to field
44+
* wrong behavior when there are no fields in json - these fields will be updated to null anyway

0 commit comments

Comments
 (0)