Skip to content

Commit efbd245

Browse files
author
rfadatare
committed
Spring boot 2 tutorial examples
0 parents  commit efbd245

File tree

198 files changed

+8444
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

198 files changed

+8444
-0
lines changed

spring-boot-crud-rest/.gitignore

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.3/apache-maven-3.5.3-bin.zip

spring-boot-crud-rest/api-documents

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
------------------- User Rest API'S details -----------------------------
2+
Add Collector - http://localhost:8080/springbootcrudrest/api/v1/users
3+
4+
Method : POST
5+
6+
Sample Request Json :
7+
{
8+
"key": 123456,
9+
"logKey": 1234568,
10+
"latitude": "sample latitude",
11+
"longitude": "sample longitude"
12+
}
13+
14+
Sample Response Json :
15+
{
16+
"id": 1,
17+
"key": 123456,
18+
"logKey": 1234568,
19+
"latitude": "sample latitude",
20+
"longitude": "sample longitude",
21+
"createdAt": 1516197019198,
22+
"createdBy": "sm_user",
23+
"updatedAt": 1516197019198,
24+
"updatedby": "sm_user"
25+
}
26+
--------------------------------------------------------------------------
27+
Update Collector - http://localhost:8080/springbootcrudrest/api/v1/users/1
28+
29+
Method : PUT
30+
31+
Sample Request Json :
32+
33+
{
34+
"key": 123456,
35+
"logKey": 54321,
36+
"latitude": "update latitude",
37+
"longitude": "update longitude "
38+
}
39+
Sample Response Json :
40+
{
41+
"id": 1,
42+
"key": 123456,
43+
"logKey": 123456,
44+
"latitude": "update latitude",
45+
"longitude": "update longitude ",
46+
"createdAt": 1516195877000,
47+
"createdBy": "sm_user",
48+
"updatedAt": 1516196212491,
49+
"updatedby": "sm_user"
50+
}
51+
--------------------------------------------------------------------------
52+
Get Collector - http://localhost:8080/springbootcrudrest/api/v1/users/1
53+
54+
Method : GET
55+
56+
Sample Request Json :
57+
Sample Response Json :
58+
{
59+
"id": 1,
60+
"key": 123456,
61+
"logKey": 123456,
62+
"latitude": "update latitude",
63+
"longitude": "update longitude ",
64+
"createdAt": 1516195877000,
65+
"createdBy": "sm_user",
66+
"updatedAt": 1516196212000,
67+
"updatedby": "sm_user"
68+
}
69+
70+
--------------------------------------------------------------------------
71+
Get Collectors - http://localhost:8080/springbootcrudrest/api/v1/users
72+
73+
Method : GET
74+
75+
Sample Request Json :
76+
Sample Response Json :
77+
[
78+
{
79+
"id": 1,
80+
"key": 123456,
81+
"logKey": 123456,
82+
"latitude": "update latitude",
83+
"longitude": "update longitude ",
84+
"createdAt": 1516197019000,
85+
"createdBy": "sm_user",
86+
"updatedAt": 1516197078000,
87+
"updatedby": "sm_user"
88+
},
89+
{
90+
"id": 2,
91+
"key": 123456,
92+
"logKey": 1234568,
93+
"latitude": "sample latitude",
94+
"longitude": "sample longitude",
95+
"createdAt": 1516197065000,
96+
"createdBy": "sm_user",
97+
"updatedAt": 1516197065000,
98+
"updatedby": "sm_user"
99+
}
100+
]
101+
--------------------------------------------------------------------------
102+
Delete Collector - http://localhost:8080/springbootcrudrest/api/v1/users/1
103+
104+
Method : DELETE
105+
106+
Sample Request Json :
107+
Sample Response Json :{deleted : true}

spring-boot-crud-rest/pom.xml

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.companyname</groupId>
7+
<artifactId>springbootcrudrest</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>springbootcrudrest</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.4.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-devtools</artifactId>
40+
<scope>runtime</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>mysql</groupId>
44+
<artifactId>mysql-connector-java</artifactId>
45+
<scope>runtime</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-test</artifactId>
50+
<scope>test</scope>
51+
</dependency>
52+
</dependencies>
53+
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-maven-plugin</artifactId>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
63+
64+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.companyname.springbootcrudrest;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
6+
7+
@SpringBootApplication
8+
@EnableJpaAuditing
9+
public class SpringBootCrudRestApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(SpringBootCrudRestApplication.class, args);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.companyname.springbootcrudrest.controller;
2+
3+
import java.util.Date;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import javax.validation.Valid;
9+
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.http.ResponseEntity;
12+
import org.springframework.web.bind.annotation.DeleteMapping;
13+
import org.springframework.web.bind.annotation.GetMapping;
14+
import org.springframework.web.bind.annotation.PathVariable;
15+
import org.springframework.web.bind.annotation.PostMapping;
16+
import org.springframework.web.bind.annotation.PutMapping;
17+
import org.springframework.web.bind.annotation.RequestBody;
18+
import org.springframework.web.bind.annotation.RequestMapping;
19+
import org.springframework.web.bind.annotation.RestController;
20+
21+
import com.companyname.springbootcrudrest.exception.ResourceNotFoundException;
22+
import com.companyname.springbootcrudrest.model.User;
23+
import com.companyname.springbootcrudrest.repository.UserRepository;
24+
25+
@RestController
26+
@RequestMapping("/api/v1")
27+
public class UserController {
28+
29+
@Autowired
30+
private UserRepository userRepository;
31+
32+
33+
@GetMapping("/users")
34+
public List<User> getAllUsers() {
35+
return userRepository.findAll();
36+
}
37+
38+
@GetMapping("/users/{id}")
39+
public ResponseEntity<User> getUserById(
40+
@PathVariable(value = "id") Long userId) throws ResourceNotFoundException {
41+
User user = userRepository.findById(userId)
42+
.orElseThrow(() -> new ResourceNotFoundException("User not found :: " + userId));
43+
return ResponseEntity.ok().body(user);
44+
}
45+
46+
@PostMapping("/users")
47+
public User createUser(@Valid @RequestBody User user) {
48+
return userRepository.save(user);
49+
}
50+
51+
@PutMapping("/users/{id}")
52+
public ResponseEntity<User> updateUser(
53+
@PathVariable(value = "id") Long userId,
54+
@Valid @RequestBody User userDetails) throws ResourceNotFoundException {
55+
User user = userRepository.findById(userId)
56+
.orElseThrow(() -> new ResourceNotFoundException("User not found :: " + userId));
57+
58+
user.setEmailId(userDetails.getEmailId());
59+
user.setLastName(userDetails.getLastName());
60+
user.setFirstName(userDetails.getFirstName());
61+
user.setUpdatedAt(new Date());
62+
final User updatedUser = userRepository.save(user);
63+
return ResponseEntity.ok(updatedUser);
64+
}
65+
66+
@DeleteMapping("/users/{id}")
67+
public Map<String, Boolean> deleteUser(
68+
@PathVariable(value = "id") Long userId) throws ResourceNotFoundException {
69+
User user = userRepository.findById(userId)
70+
.orElseThrow(() -> new ResourceNotFoundException("User not found :: " + userId));
71+
72+
userRepository.delete(user);
73+
Map<String, Boolean> response = new HashMap<>();
74+
response.put("deleted", Boolean.TRUE);
75+
return response;
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.companyname.springbootcrudrest.exception;
2+
3+
import java.util.Date;
4+
5+
public class ErrorDetails {
6+
private Date timestamp;
7+
private String message;
8+
private String details;
9+
10+
public ErrorDetails(Date timestamp, String message, String details) {
11+
super();
12+
this.timestamp = timestamp;
13+
this.message = message;
14+
this.details = details;
15+
}
16+
17+
public Date getTimestamp() {
18+
return timestamp;
19+
}
20+
21+
public String getMessage() {
22+
return message;
23+
}
24+
25+
public String getDetails() {
26+
return details;
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.companyname.springbootcrudrest.exception;
2+
3+
import java.util.Date;
4+
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.ControllerAdvice;
8+
import org.springframework.web.bind.annotation.ExceptionHandler;
9+
import org.springframework.web.context.request.WebRequest;
10+
11+
@ControllerAdvice
12+
public class GlobalExceptionHandler {
13+
@ExceptionHandler(ResourceNotFoundException.class)
14+
public ResponseEntity<?> resourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
15+
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
16+
return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
17+
}
18+
19+
@ExceptionHandler(Exception.class)
20+
public ResponseEntity<?> globleExcpetionHandler(Exception ex, WebRequest request) {
21+
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
22+
return new ResponseEntity<>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.companyname.springbootcrudrest.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
@ResponseStatus(value = HttpStatus.NOT_FOUND)
7+
public class ResourceNotFoundException extends Exception{
8+
9+
private static final long serialVersionUID = 1L;
10+
11+
public ResourceNotFoundException(String message){
12+
super(message);
13+
}
14+
}

0 commit comments

Comments
 (0)