Web Framework) helloRestAPI 실습-1

Jay Kim·2022년 1월 17일
0

Web Framework

목록 보기
5/10

<1>


1. New -> project -> spring Legacy project에서 helloRest 프로젝스 생성하기

2.pom.xml 수정

- groupId , artifactId, version 수정

- properties 버전 수정

<properties>
  <java-version>1.11</java-version>
  <org.springframework-version>5.2.6.RELEASE</org.springframework-version>
  <org.aspectj-version>1.6.10</org.aspectj-version>
  <org.slf4j-version>1.7.30</org.slf4j-version>
</properties>

- dependency 추가

(1) jackson-databind

<!--  jackson-databind -->
	<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.12.1</version>
	</dependency>

(2) spring transaction

<!-- spring-tx -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

(3) lombok

	<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.8</version>
			<scope>provided</scope>
	</dependency>
 

(4) servlet 관련 dependency 수정

<!-- Servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>javax.servlet.jsp-api</artifactId>
			<version>2.3.1</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

(5) plugin configuration 수정

	<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<source>1.11</source>
					<target>1.11</target>
					<compilerArgument>-Xlint:all</compilerArgument>
					<showWarnings>true</showWarnings>
					<showDeprecation>true</showDeprecation>
				</configuration>
	</plugin>

(6) pom.xml 수정하였으니 Maven update project 해주기


<2> package 생성 - controller/ model/ service

1.model 패키지 안에 User.java 클래스 생성

package kr.ac.hansung.cse.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@AllArgsConstructor		//생성자 만들 시 아래의 필드들 모두 갖도록 하기 위함
public class User {

	private long id;
	private String name;
	private int age;
	private double salary;
	
}

2.service 패키지 안에 UserService.java 클래스 생성

package kr.ac.hansung.cse.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import kr.ac.hansung.cse.model.User;

@Service
@Transactional
public class UserService {
	
	private static final AtomicLong counter = new AtomicLong();	//AtomicLong 사용은 상호배제를 위함
	
	private static List<User> users;
	
	public UserService() {	//생성자
		users = new ArrayList<User>();
	
		users.add(new User(counter.incrementAndGet(), "Alice",30, 70000));
		users.add(new User(counter.incrementAndGet(), "Bob",40, 50000));
		users.add(new User(counter.incrementAndGet(), "Charlie",45, 30000));
		users.add(new User(counter.incrementAndGet(), "Daniel",50, 40000));
	}
	
	public List<User> findAllUsers() {
		return users;
	}
	
	public User findById(long id) {
		for(User user : users) {
			if(user.getId() == id) {
				return user;
			}
		}
		return null;
	}
	
	public User findByName(String name) {
		for(User user : users) {
			if(user.getName().equalsIgnoreCase(name)) {
				return user;
			}
		}
		return null;
	}
	
	public void saveUser(User user) {
		user.setId(counter.incrementAndGet());
		users.add(user);
	}
	
	public void updateUser(User user) {
		int index = users.indexOf(user);
		users.set(index, user);
	}
	
	public void deleteUserById(long id) {
		
		for(Iterator<User> iterator = users.iterator(); iterator.hasNext();)	{
			User user = iterator.next();
			if(user.getId()==id)	{
				iterator.remove();
			}
		}
	}
	
	public boolean doesUserExist(User user) {
		return findByName(user.getName()) != null;
	}
	
	public void deleteAllUsers()	{
		users.clear();
	}
	
	

}

0개의 댓글