Java/Tools
[java] WSDL 파일 읽고 자바코드 생성
허니몬
2017. 5. 11. 18:42
해외 기업의 웹서비스를 이용하는 기능을 개발하고 있다. 이 과정에서 낯설은 wsdl
생성과정 및 SOAP 사용방법을 정리해보고자 한다.
wsimport
는 JAX-WS 에 적합한 산출물을 생성하는 도구다. wsdl(Web Services Description Language) 을 불러와 그 파일을 기준으로 자바 코드를 생성한다.
사용방법
사용방법은 간단하다(물론 옵션은 여러가지가 있다. 상황에 따라 적절한 옵션을 추가하자).
$ wsimport {wsdl-url}
선택사항
$ wsimportwsimportMissing WSDL_URIUsage: wsimport <WSDL_URI>where include:-b <path> specify jaxws/jaxb binding files or additional schemas-B<jaxbOption> Pass this option to JAXB schema compiler-catalog <file> specify catalog file to resolve external entity referencessupports 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 specifiedby the specification. Use of extensions mayresult in applications that are not portable ormay not interoperate with other implementations-help display help-httpproxy:<proxy> set a HTTP proxy. Format is proxyHost:proxyPort-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 versionDefaults to 2.2, Accepted values are 2.0, 2.1 and 2.2e.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 theWSDL 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 implementationExtensions:-XadditionalHeaders map headers not bound to request or response message toJava method parameters-Xauthfile file to carry authorization information in the formathttp://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 ignoredwsdlsExamples:wsimport stock.wsdl -b stock.xml -b stock.xjb
실습
http://www.webservicex.com/globalweather.asmx?WSDL
을 기준으로 테스트를 해보자.
$ wsimport -verbose -keep -extension http://www.webservicex.com/globalweather.asmx\?WSDL
라고 실행하면
parsing WSDL...SOAP port "GlobalWeatherSoap12": uses a non-standard SOAP 1.2 binding.line 199 of http://www.webservicex.com/globalweather.asmx?WSDLPort "GlobalWeatherHttpGet" is not a SOAP port, it has no soap:addressline 202 of http://www.webservicex.com/globalweather.asmx?WSDLport "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?WSDLPort "GlobalWeatherHttpPost" is not a SOAP port, it has no soap:addressline 205 of http://www.webservicex.com/globalweather.asmx?WSDLport "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?WSDLGenerating code...net/webservicex/GetCitiesByCountry.javanet/webservicex/GetCitiesByCountryResponse.javanet/webservicex/GetWeather.javanet/webservicex/GetWeatherResponse.javanet/webservicex/GlobalWeather.javanet/webservicex/GlobalWeatherHttpGet.javanet/webservicex/GlobalWeatherHttpPost.javanet/webservicex/GlobalWeatherSoap.javanet/webservicex/ObjectFactory.javanet/webservicex/package-info.javaCompiling 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
참고문헌
GlobalWeather sample wsdl:
http://www.webservicex.com/globalweather.asmx?WSDL
wsimport:
https://docs.oracle.com/javase/6/docs/technotes/tools/share/wsimport.html