The method of generating java code according to wsdl

  I checked a very strange problem of a colleague today, and now I will list the problems encountered today.

        When calling the webservice service of the peripheral system, generally speaking, the other party will provide a wsdl, and then we need to generate the corresponding class according to the wsdl. In this way, a perfect cross-language solution is achieved, but it also brings some complexity. In java, we generally use the wsimport tool that comes with jdk or the wsdl4j tool of other third-party systems to generate the stub implementation of the java version.

        

  •    Use the wsimport tool that comes with jdk. The specific usage is as follows:

        In the bin folder of the JDK, there is a wsimport.exe. This tool generates corresponding class files based on the wsdl file, and then using these class files, you can call the method provided by WebService just like calling a local class. This tool can be used for non-Java servers, such as WebServices written in C#, and Java client implementations are generated through wsimport.

  Use the following command in the command prompt:

       wsimport http://localhost:8080/Demo/services/MyService?wsdl

 

  Command parameter description:
  -d: The storage directory of the class file that generates the client execution class
  -s: The storage directory of the source file that generates the client execution class
  -p: Define the package name of the generated class
  For other command parameters, please refer to: http://docs.oracle.com/javase/6/docs/technotes/tools/share/wsimport.html
  After using the wsimport command, you can get the code, and then you can copy the code into the project, or compile and package it into a jar.
      比如 javac com/xxx/*.java com/xxx/yyy/*.java
              jar cvf dest.jar com /
              This way we get a dest.jar file
        Use the wsdl4j generated by the webservice framework. Due to well-known reasons, there are many jars of java webservices. Often due to different versions and implementations, there will be some inexplicable problems, so it is recommended to use the one you use here. The framework's wsdl4j tool generates the corresponding stub.
 
  •   Use a third-party ws framework, take cxf as an example:

       Use the wsdl4j generation that comes with the webservice framework. Due to well-known reasons, there are many java webservice frameworks. Often due to different versions and implementations, there will be some inexplicable problems , so it is recommended to use the one you use here. The framework's wsdl4j tool generates the corresponding stub .

 

  1.  Download apache-cxf-2.5.11.zip and extract it to D:\apache-cxf-2.5.11, download address: http://mirror.bjtu.edu.cn/apache/cxf/2.5.11/apache-cxf -2.5.11.tar.gz 
  2. Execute the command under the D:\apache-cxf-2.5.11\bin directory: wsdl2java   http://xxx.yyy.com:8102/ws/UserRemoteService?wsdl     
  3. After the command is executed, the corresponding code will be generated in the relevant folder, and all the code will be imported into the access system, which is the same as wsimport

   Problems encountered during use:

  •     A problem was found in the process of use. Since jdk6 only supports the ws2.1 specification version at most, when generating under jdk6, add -frontend jaxws21

        wsdl2java -frontend jaxws21  http://xxx.yyy.com:8102/ws/UserRemoteService?wsdl    

 

  • 发现可以根据wsdl生成java类,java类中的注释有可能是中文,所以要加上encoding参数,例如:-encoding utf-8  

http://asialee.iteye.com/blog/1912953

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326768220&siteId=291194637