Home > 개발 > Spring Boot > Spring Boot + Maven 환경에서 커스텀 빌드하기 (3)

Spring Boot + Maven 환경에서 커스텀 빌드하기 (3)

Spring Boot + Maven 환경에서 커스텀 빌드하기 (3)


CUSTOM REST API 예제 작성

고객사 전용 커스텀 예제를 작성한다.

마찬가지로 최대한 간단하게 작성한다. (이게 중요한 게 아니니까2)

Test Code

package com.toy.springbootmavencustombuild.site.customer;

import com.toy.springbootmavencustombuild.site.customer.rest.CustomerController;
import com.toy.springbootmavencustombuild.site.customer.service.CustomerService;
import org.junit.Before;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.BDDMockito.given;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * CustomerController 테스트 코드
 *
 * @author readra
 */
@AutoConfigureRestDocs
@WebMvcTest(CustomerController.class)
public class CustomerControllerTest {
	@Autowired
	private MockMvc mockMvc;
	@Autowired
	private WebApplicationContext webApplicationContext;
	@MockBean
	private CustomerService customerService;

	@Before
	public void setUp() {
		this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext)
				.apply(documentationConfiguration(new JUnitRestDocumentation()))
				.build();
	}

	@Test
	@DisplayName("Customer Hello World")
	void helloWorldTest() throws Exception {
		final int code = 100;
		final String result = String.format(CustomerService.HELLO_WORLD, code);

		// given
		given(customerService.helloWorld(anyInt())).willReturn(result);

		// when
		ResultActions resultActions = mockMvc.perform(
				RestDocumentationRequestBuilders.get("/v1/customer/hello-world/{code}", code)
						.contentType(MediaType.APPLICATION_JSON)
		);

		// then
		resultActions.andExpect(status().isOk())
				.andDo(
						document(
								"Customer Hello World",
								preprocessRequest(prettyPrint()),
								preprocessResponse(prettyPrint()),
								pathParameters(
										parameterWithName("code").description("코드")
								),
								responseFields(
										fieldWithPath("message").type(JsonFieldType.STRING).description("메시지"),
										fieldWithPath("code").type(JsonFieldType.NUMBER).description("코드")
								)
						)

				)
				.andDo(print());
	}
}

Controller

package com.toy.springbootmavencustombuild.site.customer.rest;

import com.toy.springbootmavencustombuild.site.customer.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 고객사 전용 Controller Layer
 *
 * @author readra
 */
@RestController
@RequestMapping("/v1/customer/")
public class CustomerController {
	private final CustomerService customerService;

	@Autowired
	public CustomerController(final CustomerService customerService) {
		this.customerService = customerService;
	}

	/**
	 * 안녕하세요!
	 *
	 * @param code
	 * 		코드
	 * @return
	 *      안녕하세요!
	 */
	@GetMapping("/hello-world/{code}")
	public String helloWorld(@PathVariable int code) {
		return customerService.helloWorld(code);
	}
}

Service

package com.toy.springbootmavencustombuild.site.customer.service;

import org.springframework.stereotype.Service;

/**
 * 고객사 전용 Service Layer
 *
 * @author readra
 */
@Service
public class CustomerService {
	public static final String HELLO_WORLD = "{ \"message\" : \"Hello World My Customer\", \"code\" : %d }";

	/**
	 * 안녕하세요!
	 *
	 * @param code
	 * 		코드
	 * @return
	 *      안녕하세요!
	 */
	public String helloWorld(int code) {
		return String.format(HELLO_WORLD, code);
	}
}

REST Docs

= Rest Docs Customer Sample API Document
readra.github.io
:doctype: book
:icons: font
:source-highlighter: highlightjs
:toc: left
:toclevels: 2
:sectlinks:

[[introduction]]
== 소개
readra Spring Rest Docs API

---

=== 고객사 전용 안녕하세요 API
고객사 전용 안녕하세요 API를 소개합니다.

operation::Customer Hello World[snippets='http-request,path-parameters,http-response,response-fields,curl-request']

---