헤르메스 LIFE

[Spring Boot] Spring Boot Sample REST 본문

Spring Boot Framework

[Spring Boot] Spring Boot Sample REST

헤르메스의날개 2021. 1. 8. 02:04
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