Skip to content

Commit 208c39a

Browse files
committed
initial commit: set up Spring Boot REST API with user management features
0 parents  commit 208c39a

File tree

22 files changed

+248
-0
lines changed

22 files changed

+248
-0
lines changed

pom.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.example</groupId>
7+
<artifactId>java-api</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
<name>java-api-with-otlp-sdk</name>
11+
<description>Exemplo de API REST em Java com Spring Boot</description>
12+
13+
<parent>
14+
<groupId>org.springframework.boot</groupId>
15+
<artifactId>spring-boot-starter-parent</artifactId>
16+
<version>3.1.0</version>
17+
<relativePath/>
18+
</parent>
19+
20+
<properties>
21+
<java.version>17</java.version>
22+
</properties>
23+
24+
<dependencies>
25+
<!-- Spring Boot Starter Web para construir APIs REST -->
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
31+
<!-- Spring Data JPA -->
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-data-jpa</artifactId>
35+
</dependency>
36+
37+
<!-- In-memory H2 Database -->
38+
<dependency>
39+
<groupId>com.h2database</groupId>
40+
<artifactId>h2</artifactId>
41+
<scope>runtime</scope>
42+
</dependency>
43+
44+
<!-- Swagger UI com SpringDoc -->
45+
<dependency>
46+
<groupId>org.springdoc</groupId>
47+
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
48+
<version>2.1.0</version>
49+
</dependency>
50+
</dependencies>
51+
52+
<build>
53+
<plugins>
54+
<!-- Plugin do Spring Boot -->
55+
<plugin>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-maven-plugin</artifactId>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class RestApiApplication {
8+
public static void main(String[] args) {
9+
SpringApplication.run(RestApiApplication.class, args);
10+
}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.controller;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
@RestController
7+
public class HelloController {
8+
9+
@GetMapping("/hello")
10+
public String hello() {
11+
return "Hello, World!";
12+
}
13+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.example.controller;
2+
3+
import com.example.model.User;
4+
import com.example.service.UserService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import java.util.List;
9+
10+
@RestController
11+
@RequestMapping("/users")
12+
public class UserController {
13+
14+
@Autowired
15+
private UserService userService;
16+
17+
@GetMapping
18+
public List<User> getAllUsers() {
19+
return userService.getAllUsers();
20+
}
21+
22+
@GetMapping("/{id}")
23+
public User getUserById(@PathVariable Long id) {
24+
return userService.getUserById(id);
25+
}
26+
27+
@PostMapping
28+
public User createUser(@RequestBody User user) {
29+
return userService.createUser(user);
30+
}
31+
32+
@PutMapping("/{id}")
33+
public User updateUser(@PathVariable Long id, @RequestBody User user) {
34+
return userService.updateUser(id, user);
35+
}
36+
37+
@DeleteMapping("/{id}")
38+
public void deleteUser(@PathVariable Long id) {
39+
userService.deleteUser(id);
40+
}
41+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
@ResponseStatus(HttpStatus.NOT_FOUND)
7+
public class UserNotFoundException extends RuntimeException {
8+
public UserNotFoundException(String message) {
9+
super(message);
10+
}
11+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.example.model;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.GenerationType;
6+
import jakarta.persistence.Id;
7+
import jakarta.persistence.Table;
8+
9+
@Entity
10+
@Table(name = "users")
11+
public class User {
12+
@Id
13+
@GeneratedValue(strategy = GenerationType.IDENTITY)
14+
private Long id;
15+
16+
private String name;
17+
private String email;
18+
19+
// Getters e Setters
20+
public Long getId() {
21+
return id;
22+
}
23+
public void setId(Long id) {
24+
this.id = id;
25+
}
26+
public String getName() {
27+
return name;
28+
}
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
public String getEmail() {
33+
return email;
34+
}
35+
public void setEmail(String email) {
36+
this.email = email;
37+
}
38+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.example.repository;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import com.example.model.User;
5+
6+
public interface UserRepository extends JpaRepository<User, Long> {
7+
// Adicione aqui os métodos customizados, se necessário.
8+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.example.service;
2+
3+
import com.example.model.User;
4+
import com.example.repository.UserRepository;
5+
import com.example.exception.UserNotFoundException;
6+
import org.springframework.stereotype.Service;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
9+
import java.util.List;
10+
11+
@Service
12+
public class UserService {
13+
14+
@Autowired
15+
private UserRepository userRepository;
16+
17+
public List<User> getAllUsers() {
18+
return userRepository.findAll();
19+
}
20+
21+
public User getUserById(Long id) {
22+
return userRepository.findById(id)
23+
.orElseThrow(() -> new UserNotFoundException("User not found with id " + id));
24+
}
25+
26+
public User createUser(User user) {
27+
return userRepository.save(user);
28+
}
29+
30+
public User updateUser(Long id, User userDetails) {
31+
User user = getUserById(id);
32+
user.setName(userDetails.getName());
33+
user.setEmail(userDetails.getEmail());
34+
return userRepository.save(user);
35+
}
36+
37+
public void deleteUser(Long id) {
38+
userRepository.deleteById(id);
39+
}
40+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring.h2.console.enabled=true
2+
spring.datasource.url=jdbc:h2:mem:testdb
3+
spring.datasource.driverClassName=org.h2.Driver
4+
spring.jpa.hibernate.ddl-auto=update
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring.h2.console.enabled=true
2+
spring.datasource.url=jdbc:h2:mem:testdb
3+
spring.datasource.driverClassName=org.h2.Driver
4+
spring.jpa.hibernate.ddl-auto=update

0 commit comments

Comments
 (0)