250x250
Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- Python
- myBatis
- JDBC
- PostgreSQL
- Core Java
- Exception
- 오픈소스
- 문서
- Open Source
- SpringBoot
- git
- AJAX
- MySQL
- Eclipse
- Tomcat
- 설정
- IntelliJ
- Source
- Docker
- MSSQL
- oracle
- error
- spring
- Thymeleaf
- jpa
- STS
- JavaScript
- ubuntu
- maven
- Spring Boot
Archives
- Today
- Total
헤르메스 LIFE
[Spring Boot] Spring Boot Sample REST 본문
728x90
개발환경
Spring Boot 2.2.4
JDK 1.8.0_202
REST
Postman : REST Client
목표
1. Spring Boot REST 환경
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.annotation.PostConstruct;
import java.util.TimeZone;
@SpringBootApplication
public class Application {
@PostConstruct
void init() {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
import com.sample.model.User;
import com.sample.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("")
public List<User> getAllUsers() {
System.out.println("[UserController >> getAllUsers]");
List<User> result = userService.getAllUsers();
return result;
}
@GetMapping(value = "/{userId}")
public User getUserbyUserId(@PathVariable String userId) {
System.out.println("[UserController >> getUserbyUserId]");
return userService.getUserbyUserId(userId);
}
@PostMapping("")
//@RequestMapping(value = "", method = {RequestMethod.GET, RequestMethod.PUT})
public List<User> insertUser(@RequestBody User user) {
System.out.println("[UserController >> insertUser] user :: " + user);
//User users = new User("1010", "임꺽정");
return userService.insertUser(user);
}
@PutMapping("")
public List<User> updateUser(@RequestBody User user) {
User users = new User("1010", "임꺽정");
System.out.println("[UserController >> updateUser]");
return userService.updateUser(user);
}
@DeleteMapping("")
public List<User> deleteUser() {
System.out.println("[UserController >> deleteUser]");
return userService.deleteUser("1010");
}
}
import com.sample.model.User;
import java.util.List;
import java.util.Map;
public interface UserService {
public List<User> getAllUsers();
public User getUserbyUserId(String userId);
public List<User> insertUser(User user);
public List<User> updateUser(User user);
public List<User> deleteUser(String userId);
public Map<String, String> getMessage();
}
import com.sample.dao.UserRepository;
import com.sample.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public List<User> getAllUsers() {
System.out.println("[UserServiceImpl >> getAllUsers]");
return userRepository.getAllUsers();
}
@Override
public User getUserbyUserId(String userId) {
System.out.println("[UserServiceImpl >> getUserbyUserId]");
return userRepository.getUserbyUserId(userId);
}
@Override
public List<User> insertUser(User param) {
System.out.println("[UserServiceImpl >> insertUser]");
return userRepository.insertUser(param);
}
@Override
public List<User> updateUser(User param) {
System.out.println("[UserServiceImpl >> updateUser]");
return userRepository.updateUser(param);
}
@Override
public List<User> deleteUser(String userId) {
System.out.println("[UserServiceImpl >> deleteUser]");
return userRepository.deleteUser(userId);
}
@Override
public Map<String, String> getMessage() {
Map<String, String> result = new HashMap<String, String>();
result.put("errorCode", "S");
result.put("description", "Success");
return result;
}
}
import com.sample.model.User;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
@Repository
public class UserRepository {
public static List<User> users;
static {
users = new ArrayList<>();
users.add(new User("1001", "홍길동"));
users.add(new User("1002", "허균"));
users.add(new User("1003", "신사임당"));
users.add(new User("1004", "허난설헌"));
}
public List<User> getAllUsers() {
System.out.println("[UserRepository >> getAllUsers]");
return users;
}
public User getUserbyUserId(String userId) {
System.out.println("[UserRepository >> getUserbyUserId] userId :: " + userId);
return users.stream().filter(user -> user.getUserId().equals(userId)).findAny().orElse(new User("", "직원없음"));
}
public List<User> insertUser(User param) {
System.out.println("[UserRepository >> insertUser] param :: " + param);
users.add(param);
return users;
}
public List<User> updateUser(User param) {
System.out.println("[UserRepository >> updateUser] param :: " + param);
User userInfo = users.stream().filter(user -> user.getUserId().equals(param.getUserId())).findAny().get();
userInfo.setName(param.getName());
return users;
}
public List<User> deleteUser(String userId) {
System.out.println("[UserRepository >> deleteUser] userId :: " + userId);
users.removeIf(user -> user.getUserId().equals(userId));
return users;
}
}
application.properties
server.port = 8090
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.mvc.hiddenmethod.filter.enabled=true
logging.level.org.springframework=ERROR
logging.level.com.acompany.restapp=DEBUG
<?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>
<groupId>org.example</groupId>
<artifactId>SpringBootSample</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<!-- 빌드 플러그인 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
728x90
'Spring Boot Framework' 카테고리의 다른 글
[Spring Boot] JUnit Test (0) | 2021.01.17 |
---|---|
[Spring Boot] Spring Boot + JWT ( JSON Web Token ) + DB 연결 (0) | 2021.01.14 |
[Spring Boot] Spring Boot + JWT ( JSON Web Token ) 테스트 (0) | 2021.01.10 |
[Spring Boot] Spring Boot Sample REST + Log4J2 (0) | 2021.01.09 |
[Spring Boot] IntelliJ로 간단한 Spring Boot 환경 테스트 (0) | 2020.12.26 |