memostack
article thumbnail
SpringBoot, Junit5에서 REST API(Controller) 테스트
Backend/Spring Boot 2021. 1. 15. 12:06

REST API 테스트 Rest API를 테스트할 때는 @WebMvcTest 어노테이션을 사용한다. MockMvc를 생성할때는 MockMvcBuilders를 사용하여 생성한다. 다른 방법으로 MockMvcBuilders를 사용하지 않고, MockMvc에 @Autowired를 사용하여 자동으로 생성하여 주입할 수 있다. @ExtendWith(SpringExtension.class) @WebMvcTest(MemberController.class) @DisplayName("MemberController 테스트") class MemberControllerTest { private MockMvc mvc; @MockBean private MemberService memberService; @BeforeEach pu..

article thumbnail
SpringBoot, Junit5에서 Service 테스트
Backend/Spring Boot 2021. 1. 13. 22:17

서비스 작성 MemberService.java 생성자 주입을 통해, MemberRepository와 BCryptPasswordEncoder를 주입한다. BCryptPasswordEncoder는 시큐리티에 Bean을 생성했다. (아래 펼쳐서 확인) 더보기 @Configuration @EnableWebSecurity @EnableGlobalAuthentication public class SecurityConfig extends WebSecurityConfigurerAdapter { /** * 비밀번호 암호화 인코딩 방식 정의 * * @return BCryptPasswordEncoder BCrypt 단방향 암호화 */ @Bean public BCryptPasswordEncoder passwordEncoder..

article thumbnail
SpringBoot, Junit5에서 JPA Repository 테스트
Backend/Spring Boot 2021. 1. 6. 17:10

사전 준비 테스트를 위해, Entitiy와 Repository를 생성한다. Entity 생성 Role.java @Entity @Table(name = "role") @Getter @Builder @NoArgsConstructor @AllArgsConstructor(access = AccessLevel.PROTECTED) @ToString public class Role { // PK @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; // 권한 이름 @Column(nullable = false, length = 50) private String roleName; } Repository 생성 RoleRepository.java ..

article thumbnail
Spring Boot 프로젝트에 h2 database 설정
Backend/Spring Boot 2021. 1. 5. 14:54

실행환경 SpringBoot Maven adoptopenjdk1.8 dependency 추가 version 1.4.197을 사용한다. 최근 버전에서는 SpringBoot를 실행하면서 보안상의 이유로 database를 생성하지 못하도록 막아놓아서, 먼저 DB를 생성하고 실행해야하는 번거로움이 있다. 어차피 실제로 서비스할때는 h2 DB는 테스트용으로 사용할 예정이므로, 1.4.197 버전 (예전 버전)을 사용한다. ... com.h2database h2 1.4.197 runtime ... application.yaml 설정 application.properties로 해도 상관없으나, 본 글에서는 yaml 파일로 설정 spring: h2: console: enabled: true datasource: pla..

article thumbnail
SpringBoot와 @PostMapping, @RequestBody 예제
Backend/Spring Boot 2020. 11. 11. 20:25

@PostMapping POST 통신을 할때는 @RequestMapping(method=RequestMethod.POST, ...) 를 이용하거나 @PostMapping을 이용한다. @RestController @RequestMapping("api") public class PostController { @RequestMapping(method = RequestMethod.POST, path = "/postMethod") public String postMethod() { return "Hello Spring"; } @PostMapping("/postMethod2") public String postMethod2() { return "Hello Spring"; } } @RequestBody 만약, 함께 ..

article thumbnail
Spring Boot와 @RequestMapping, @GetMapping, @RequestParam 예제
Backend/Spring Boot 2020. 11. 11. 19:57

예제를 위해 간단한 Spring Boot 프로젝트를 생성한다. 2020/02/18 - [Spring/Spring Boot] - Spring Boot 프로젝트 생성하기 Spring Boot 프로젝트 생성하기 스프링 부트 프로젝트 생성 스프링 부트 프로젝트를 생성 할 수 있게 도와주는 사이트가 있다. (https://start.spring.io) 위와 같이 원하는 Version , Metadata , Dependency 를 정하여 프로젝트를 구성한다. Ge.. memostack.tistory.com Controller 생성 Controller를 하나 생성한다. 생성할때는 @RestController를 이용한다. @RestController // REST API Controller 사용한다는 것을 프레임워크에..

article thumbnail
Spring Boot 와 MySQL 연동하기 (Maven 프로젝트)
Backend/Spring Boot 2020. 10. 31. 22:35

Gradle 내용은 아래 참고 2020/11/11 - [Spring/Spring Boot] - Spring Boot 와 MySQL & JPA 연동하기 (Gradle 프로젝트) Spring Boot 와 MySQL & JPA 연동하기 (Gradle 프로젝트) MySQL 설치 설치는 아래 글 참고 2020/10/30 - [Database/RDB] - MySQL 설치하기 (Mac OSX) MySQL 설치하기 (Mac OSX) MySQL 설치 본 글에서는 Homebrew 를 이용하여 MySQL 을 설치한다. $ brew update $ brew ins.. memostack.tistory.com 의존성 추가 아래와 같이 java와 mysql 커넥터 의존성을 pom.xml에 추가한다. mysql mysql-conne..

article thumbnail
Spring Boot 에서 JPA 사용하기 (MySQL 사용)
Backend/Spring Boot 2020. 10. 31. 21:52

MySQL 설정 MySQL 설치 MySQL 설치는 아래 글 참고 2020/10/30 - [Database/RDB] - MySQL 설치하기 (Mac OSX) MySQL 설치하기 (Mac OSX) MySQL 설치 본 글에서는 Homebrew 를 이용하여 MySQL 을 설치한다. $ brew update $ brew install mysql 만약, 특정 버전을 따로 설치하고 싶다면 아래 명령어를 수행한다. 그리고, 원하는 버전의 mysql을 설치한.. memostack.tistory.com 데이터베이스 생성 우선 예제에서 사용할 데이터 베이스 및 테이블을 생성한다. 본 글에서는 TEST_DB를 생성한다. mysql> CREATE DATABASE TEST_DB -> DEFAULT CHARACTER SET UTF..

article thumbnail
Spring Boot, Maven 프로젝트에 롬복 적용하기
Backend/Spring Boot 2020. 10. 31. 20:12

Gradle 프로젝트에 롬복 적용하는 방법은 아래 글 참고 2020/03/07 - [Spring/Spring Boot] - Spring Boot 롬복(Lombok) 적용 / Gradle과 IntelliJ 사용 Spring Boot 롬복(Lombok) 적용 / Gradle과 IntelliJ 사용 롬복(Lombok) 이란? 롬복(lombok)을 이용하면 getter, setter, constructor 를 매번 생성할 필요가 없다. 롬복은 @Getter, @Setter, @NoArgsConstructor 등등 어노테이션을 추가해주는 것으로 접근 제어자, 생성자.. memostack.tistory.com pom.xml에 롬복 추가 아래 dependancy를 추가한다. ... org.projectlombok l..

article thumbnail
Spring Boot, 간단한 REST API 만들기
Backend/Spring Boot 2020. 10. 31. 00:46

dependency 추가 REST API 기능을 만들기 위해서는 spring-boot-starter-web 디펜던시가 필요하다. pom.xml에 아래 코드를 추가한다. ... org.springframework.boot spring-boot-starter-web ... 참고. mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web Maven Repository: org.springframework.boot » spring-boot-starter-web Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the def..

article thumbnail
SpringBoot 서버 포트 변경하기
Backend/Spring Boot 2020. 10. 31. 00:23

Spring Boot 서버 포트 변경 Spring Boot에서 서버 포트를 변경하는 방법은 매우 쉽다. src/main/resources/application.properties 파일에 아래 설정값을 넣어준다. server.port=8080 처음 default값은 8080이기 때문에 8080을 사용할 때는 굳이 작성하지 않아도 된다. 만약 9000으로 실행하고 싶다면 아래처럼 설정한다. server.port=9000