Java/Tools

[java] WSDL 파일 읽고 자바코드 생성

허니몬 2017. 5. 11. 18:42

해외 기업의 웹서비스를 이용하는 기능을 개발하고 있다. 이 과정에서 낯설은 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