[spring] REST Docs 사용중 urlTemplate not found. If you are using MockMvc did you use RestDocumentationRequestBuilders to build the request?

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest
public class BookControllerTest {
    private MockMvc mockMvc;
    @Rule
    public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();

    @Autowired
    WebApplicationContext wac;
    @MockBean
    BookService bookService;

    @Before
    public void setUp() {
        //mockBookController 내에 Mock 처리된 bookService를 주입하기 위해 반드시 선언해줘야 함
        MockitoAnnotations.initMocks(this);

        this.mockMvc = MockMvcBuilders.webAppContextSetup(wac)
                .apply(documentationConfiguration(this.restDocumentation))
                .alwaysDo(print())
                .build();
    }

    @Test
    public void testOptions() throws Exception {
        this.mockMvc.perform(options("/books").contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(document("books-options"));
    }

    @Test
    public void testHead() throws Exception {
        Long id = 1L;
        when(bookService.findById(id)).thenReturn(Optional.of(new Book("test-book", "test-isbn13", "test-isbn10")));

        this.mockMvc.perform(head("/books/{id}", 1).contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(MockMvcResultMatchers.status().isNoContent())
                .andDo(document("books-head"));
    }

    @Test
    public void testGet() throws Exception {
        Long id = 1L;
        when(bookService.findById(1L)).thenReturn(Optional.of(new Book("test-book", "test-isbn13", "test-isbn10")));

        this.mockMvc.perform(get("/books/{id}", id).contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.name", Is.is("test-book")))
                .andExpect(jsonPath("$.data.isbn13", Is.is("test-isbn13")))
                .andExpect(jsonPath("$.data.isbn10", Is.is("test-isbn10")))
                .andDo(document("books-get", pathParameters(
                        parameterWithName("id").description("도서 참조키")
                )));
    }
}

다음과 같이 코드를 짰다. testGet 에서 document 선언을 하는 부분에서 pathParameters 를 추가했더니 다음과 같은 오류 메시지가 출력된다.

java.lang.IllegalArgumentException: urlTemplate not found. If you are using MockMvc did you use RestDocumentationRequestBuilders to build the request?

	at org.springframework.util.Assert.notNull(Assert.java:193)
	at org.springframework.restdocs.request.PathParametersSnippet.extractUrlTemplate(PathParametersSnippet.java:132)
	at org.springframework.restdocs.request.PathParametersSnippet.extractActualParameters(PathParametersSnippet.java:119)
	at org.springframework.restdocs.request.AbstractParametersSnippet.verifyParameterDescriptors(AbstractParametersSnippet.java:95)
	at org.springframework.restdocs.request.AbstractParametersSnippet.createModel(AbstractParametersSnippet.java:79)
	at org.springframework.restdocs.request.PathParametersSnippet.createModel(PathParametersSnippet.java:104)
	at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:83)
	at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:206)
	at org.springframework.restdocs.mockmvc.RestDocumentationResultHandler.handle(RestDocumentationResultHandler.java:55)
	at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:183)
	at io.honeymon.springboot.t.bookstore.api.controller.BookControllerTest.testGet(BookControllerTest.java:90)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
	at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.restdocs.JUnitRestDocumentation$1.evaluate(JUnitRestDocumentation.java:63)
	at org.junit.rules.RunRules.evaluate(RunRules.java:20)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

찾아보니 pathParameters를 사용할거면 MockMvcBuilders 보다 RestDocumentationRequestBuilders를 이용하는 것이 좋다고 한다.

To make the path parameters available for documentation, the request must be built using one of the methods on RestDocumentationRequestBuilders rather than MockMvcRequestBuilders.

코드를 다음과 같이 변경하면 된다.

@Test
public void testGet() throws Exception {
    Long id = 1L;
    when(bookService.findById(1L)).thenReturn(Optional.of(new Book("test-book", "test-isbn13", "test-isbn10")));


    this.mockMvc.perform(MockMvcRequestBuilders.get("/books/{id}", // <1> id).contentType(MediaType.APPLICATION_JSON_UTF8))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.data.name", Is.is("test-book")))
            .andExpect(jsonPath("$.data.isbn13", Is.is("test-isbn13")))
            .andExpect(jsonPath("$.data.isbn10", Is.is("test-isbn10")))
            .andDo(document("books-get", pathParameters(
                    parameterWithName("id").description("도서 참조키")
            )));
}

@Test
    public void testGet() throws Exception {
        Long id = 1L;
        when(bookService.findById(1L)).thenReturn(Optional.of(new Book("test-book", "test-isbn13", "test-isbn10")));


        this.mockMvc.perform(RestDocumentationRequestBuilders.get("/books/{id}", // <2> id).contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.name", Is.is("test-book")))
                .andExpect(jsonPath("$.data.isbn13", Is.is("test-isbn13")))
                .andExpect(jsonPath("$.data.isbn10", Is.is("test-isbn10")))
                .andDo(document("books-get", pathParameters(
                        parameterWithName("id").description("도서 참조키")
                )));
    }
  1. MockMvcRequestBuilders 을 <2> MockMvcRequestBuilders 으로 변경하면 된다.

Flyway는 개발영역에서만 사용

이용전략

  • 개발(로컬)에는 V1__, V2__, V3__ 으로 자유롭게 업데이트 한다.

  • commit & push 전(공개 전): 하나의 파일로 통합하여 정리한 후 정상동작을 확인하고 푸시한다.

  • 스테이지, 운영 단계에 스키마 변경에는 Flyway를 사용하지 않는다.

    • 스키마 변경 전에는 DB 상태 모니터링 하면서 장인정신으로 한땀한땀 반영한다.

    • DB 락 여부 확인, 속도 저하 등을 확인하며 진행

    • 운영데이터가 많이 누적된 경우에 처리속도가 느려 락이 걸리는 등의 상황이 발생할 수 있다.

추가사항

  • H2 인-메모리 디비는 프로토타이핑 용으로 사용한다.

  • H2에서 사용하는 쿼리가 실제 운영단계에 반영되었을 때 문제발생 가능성이 높다.

  • cleanOnValidationError: 검증 중에 에러가 발생하면 자동으로 클린 처리를 함. ← 위험!!

cleanOnValidationError

Whether to automatically call clean or not when a validation error occurs.

This is exclusively intended as a convenience for development. Even tough we strongly recommend not to change migration scripts once they have been checked into SCM and run, this provides a way of dealing with this case in a smooth manner. The database will be wiped clean automatically, ensuring that the next migration will bring you back to the state checked into SCM.

Warning ! Do not enable in production !


가끔, 내 생각과는 다르게 IDE에서 멋대로 코드를 포맷팅하는 경우가 있다.

그런 때에 사용할 수 있는 기능이 있다.

인텔레제이 2017.x 기준 [Preference > Editor > Code Style] 에서 'Formatter Control'을 체크하면 된다.

이렇게 작성한 코드도

@Test
public void formatterOff() throws Exception {
List<String> arrays = new ArrayList<>(); arrays.add("Test"); arrays.add("Formatter");
}

포맷팅을 하면

@Test
public void formatterOff() throws Exception {
List<String> arrays = new ArrayList<>();
arrays.add("Test");
arrays.add("Formatter");
}

처럼 되지만 //@formatter:off~//@formatter:on 을 이용하면

@Test
public void formatterOff() throws Exception {
//@formatter:off
List<String> arrays1 = new ArrayList<>(); arrays1.add("Test"); arrays1.add("Formatter");
//@formatter:on

List<String> arrays2 = new ArrayList<>(); arrays2.add("Test"); arrays2.add("Formatter");
}

이런 코드가

@Test
public void formatterOff() throws Exception {
//@formatter:off
List<String> arrays1 = new ArrayList<>(); arrays1.add("Test"); arrays1.add("Formatter");
//@formatter:on

List<String> arrays2 = new ArrayList<>();
arrays2.add("Test");
arrays2.add("Formatter");
}

이렇게 변경된다.


메서드 체이닝을 이용해서 작성하는 경우에 유용하다.

해외 기업의 웹서비스를 이용하는 기능을 개발하고 있다. 이 과정에서 낯설은 wsdl 생성과정 및 SOAP 사용방법을 정리해보고자 한다.

wsimport 는 JAX-WS 에 적합한 산출물을 생성하는 도구다. wsdl(Web Services Description Language) 을 불러와 그 파일을 기준으로 자바 코드를 생성한다.

사용방법

사용방법은 간단하다(물론 옵션은 여러가지가 있다. 상황에 따라 적절한 옵션을 추가하자).

$ wsimport {wsdl-url}

선택사항

$ wsimport
wsimport
Missing WSDL_URI
Usage: wsimport [options] <WSDL_URI>
where [options] include:
-b <path> specify jaxws/jaxb binding files or additional schemas
(Each <path> must have its own -b)
-B<jaxbOption> Pass this option to JAXB schema compiler
-catalog <file> specify catalog file to resolve external entity references
supports TR9401, XCatalog, and OASIS XML Catalog format.
-classpath <path> specify where to find user class files and wsimport extensions
-cp <path> specify where to find user class files and wsimport extensions
-d <directory> specify where to place generated output files
-encoding <encoding> specify character encoding used by source files
-extension allow vendor extensions - functionality not specified
by the specification. Use of extensions may
result in applications that are not portable or
may not interoperate with other implementations
-help display help
-httpproxy:<proxy> set a HTTP proxy. Format is [user[:password]@]proxyHost:proxyPort
(port defaults to 8080)
-J<javacOption> pass this option to javac
-keep keep generated files
-p <pkg> specifies the target package
-quiet suppress wsimport output
-s <directory> specify where to place generated source files
-target <version> generate code as per the given JAXWS spec version
Defaults to 2.2, Accepted values are 2.0, 2.1 and 2.2
e.g. 2.0 will generate compliant code for JAXWS 2.0 spec
-verbose output messages about what the compiler is doing
-version print version information
-fullversion print full version information
-wsdllocation <location> @WebServiceClient.wsdlLocation value
-clientjar <jarfile> creates the jar file of the generated artifacts along with the
WSDL metadata required for invoking the web service.
-generateJWS generate stubbed JWS implementation file
-implDestDir <directory> specify where to generate JWS implementation file
-implServiceName <name> local portion of service name for generated JWS implementation
-implPortName <name> local portion of port name for generated JWS implementation
Extensions:
-XadditionalHeaders map headers not bound to request or response message to
Java method parameters
-Xauthfile file to carry authorization information in the format
http://username:password@example.org/stock?wsdl
-Xdebug print debug information
-Xno-addressing-databinding enable binding of W3C EndpointReferenceType to Java
-Xnocompile do not compile generated Java files
-XdisableAuthenticator disable Authenticator used by JAX-WS RI,
-Xauthfile option will be ignored if set
-XdisableSSLHostnameVerification disable the SSL Hostname verification while fetching
wsdls
Examples:
wsimport stock.wsdl -b stock.xml -b stock.xjb
wsimport -d generated http://example.org/stock?wsdl

실습

http://www.webservicex.com/globalweather.asmx?WSDL 을 기준으로 테스트를 해보자.

$ wsimport -verbose -keep -extension http://www.webservicex.com/globalweather.asmx\?WSDL

라고 실행하면

parsing WSDL...
[WARNING] SOAP port "GlobalWeatherSoap12": uses a non-standard SOAP 1.2 binding.
line 199 of http://www.webservicex.com/globalweather.asmx?WSDL
[WARNING] Port "GlobalWeatherHttpGet" is not a SOAP port, it has no soap:address
line 202 of http://www.webservicex.com/globalweather.asmx?WSDL
[WARNING] port "GlobalWeatherHttpGet": not a standard SOAP port. The generated artifacts may not work with JAX-WS runtime.
line 202 of http://www.webservicex.com/globalweather.asmx?WSDL
[WARNING] Port "GlobalWeatherHttpPost" is not a SOAP port, it has no soap:address
line 205 of http://www.webservicex.com/globalweather.asmx?WSDL
[WARNING] port "GlobalWeatherHttpPost": not a standard SOAP port. The generated artifacts may not work with JAX-WS runtime.
line 205 of http://www.webservicex.com/globalweather.asmx?WSDL
Generating code...
net/webservicex/GetCitiesByCountry.java
net/webservicex/GetCitiesByCountryResponse.java
net/webservicex/GetWeather.java
net/webservicex/GetWeatherResponse.java
net/webservicex/GlobalWeather.java
net/webservicex/GlobalWeatherHttpGet.java
net/webservicex/GlobalWeatherHttpPost.java
net/webservicex/GlobalWeatherSoap.java
net/webservicex/ObjectFactory.java
net/webservicex/package-info.java
Compiling code...
javac -d /private/tmp/test-ws/. -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/tools.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/classes -Xbootclasspath/p:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/rt.jar /private/tmp/test-ws/./net/webservicex/GetCitiesByCountry.java /private/tmp/test-ws/./net/webservicex/GetCitiesByCountryResponse.java /private/tmp/test-ws/./net/webservicex/GetWeather.java /private/tmp/test-ws/./net/webservicex/GetWeatherResponse.java /private/tmp/test-ws/./net/webservicex/GlobalWeather.java /private/tmp/test-ws/./net/webservicex/GlobalWeatherHttpGet.java /private/tmp/test-ws/./net/webservicex/GlobalWeatherHttpPost.java /private/tmp/test-ws/./net/webservicex/GlobalWeatherSoap.java /private/tmp/test-ws/./net/webservicex/ObjectFactory.java /private/tmp/test-ws/./net/webservicex/package-info.java

처럼 실행되어 있는 것을 볼 수 있을 것이다. 대상으로 하는 wsdl 파일을 내려받은 후에 이파일을 기준으로 자바코드를 생성하는 과정을 확인할 수 있다. 그리고 내려받은 자바코드를 컴파일하는 것까지 처리해준다.

생성된 디렉토리의 구조는 다음과 같다.

.
├── globalweather.asmx?WSDL
└── net
    └── webservicex
        ├── GetCitiesByCountry.class
        ├── GetCitiesByCountry.java
        ├── GetCitiesByCountryResponse.class
        ├── GetCitiesByCountryResponse.java
        ├── GetWeather.class
        ├── GetWeather.java
        ├── GetWeatherResponse.class
        ├── GetWeatherResponse.java
        ├── GlobalWeather.class
        ├── GlobalWeather.java
        ├── GlobalWeatherHttpGet.class
        ├── GlobalWeatherHttpGet.java
        ├── GlobalWeatherHttpPost.class
        ├── GlobalWeatherHttpPost.java
        ├── GlobalWeatherSoap.class
        ├── GlobalWeatherSoap.java
        ├── ObjectFactory.class
        ├── ObjectFactory.java
        ├── package-info.class
        └── package-info.java



인텔리제이 12버전인가를 사용했던 기억이 나는데...

4년만에 다시 사용하려고 하니 너무나 낯설다.

그렇다고 해서 이클립스를 잘 사용한 건 아니지만...

+ Recent posts