This is the second day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”
Weather interface address:www.webxml.com.cn/WebServices…
Generate the corresponding Java entities
- Open the project files directory path folder, open CMD in the current folder, and enter the following command:
wsimport -encoding utf-8 -keep -Xnocompile -p com.ratel.weather -wsdllocation /wsdl/weather.wsdl -verbose http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
Copy the code
Parameter description: -encoding: -keep: generates the Java source file. -d: specifies the output directory of the. Class file. -s: specifies the output directory of the. Specify a JAXWS/JAXB binding file or additional schemas-extension: Use the extension to support SOAP1.2-xnocompile: Don’t compile the generated Java files – wsdllocation: @ WebServiceClient. Wsdllocation value, this value can be specified in the configuration file into the project, easy to pack after the deployment of the migration program.
If the situation as shown below occurs:Open the weather interface address in your browser:www.webxml.com.cn/WebServices…Weather.wsdl opens the downloaded file with the editor and will
<s:element ref="s:schema" />
<s:any />
Copy the code
Instead of
<s:any minOccurs="2" maxOccurs="2" />
Copy the code
After saving, run the following command:
wsimport -encoding utf-8 -keep -Xnocompile -p com.ratel.weather -wsdllocation /wsdl/weather.wsdl -verbose weather.wsdl
Copy the code
Ii. Construction projects
Copy the generated files to a Maven project, and copy weather. WSDL to the WSDL directory of Resources (no new directory).A more important class:
Three, test,
- Create a new test class:
package com.ratel.weather;
import java.net.MalformedURLException;
import java.net.URL;
/** * @ Service description: *@package_name: com. Ratel. Weather *@project_name: weather
* @copyright(c) Ratelfu All Rights Reserved */
public class WeatherTest {
public static void main(String[] args) throws MalformedURLException {
// You can also use the new WeatherWebService(URL) method to reset the requested address
//URL url=new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");
//WeatherWebService factory = new WeatherWebService(url);
WeatherWebService factory = new WeatherWebService();
WeatherWebServiceSoap weatherWebServiceSoap = factory.getWeatherWebServiceSoap(); //WeatherWebServiceSoap is the invoked implementation class
ArrayOfString strArray = null;
strArray = weatherWebServiceSoap.getWeatherbyCityName("Zhengzhou"); System.out.println(strArray.getString()); }}Copy the code
If the result as shown below appears, the call is successful:
Jar the project and specify the main class of the runtime
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<! Set the main class to load -->
<mainClass>com.ratel.weather.WeatherTest</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
<classesDirectory>
</classesDirectory>
</configuration>
</plugin>
</plugins>
</build>
Copy the code
Packaging:
5. Test deployment migration
Find where the JAR was generated and test it:
Vi. Source code
Making: github.com/Dr-Water/we…
7. Reference links
- WebService01_02 — The client accesses wsimPort
- JAVA calling the WebService interface (Using the third-party weather interface as an example)
- WebService01_03 — how does tcpip-monitor Monitor SOAP network requests