The source code
The source code has been uploaded to Github
Generate the Jave class from WSDL
1.1 Java Environment Configuration
Download and install Java, and configure the JDK and JRE into system environment variables. (Java 1.8, Tomcat 7.0)
Because the wsimport command is used, you are advised to use JAVA_HOME and CLASS_PATH to ensure global access to the wsimport command
1.2 Obtaining the WSDL file
Use the Web service address to select the desired Web service type
Take the weather service as an example: the weather service WSDL document address
Right-click save as local and modify part of the WSDL document: replace < S: Element ref=” S :schema” />< S :any /> with < S :any minOccurs=”2″ maxOccurs=”2″/>. There are three changes in this document.
Generate the Jave class using the wsimport command
wsimport -keep -dThe output directory of the.class file-sOutput directory for.java files WSDL document locationCopy the code
After running, you can see the generated Java file in the specified output directory
The above method is for the WSDL document developed by.NET and needs to modify its content to regenerate Java files. For non-.NET developed documents, such as mobile phone attribution query, you can directly use the network address to generate Java files:
wsimport -keep -dThe output directory of the.class file-sThe output of the.java file directory http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdlCopy the code
The way files generate Java files, do not delete or change the location of the.wsdl file, because the location of the.wsdl file will be written to the Java file and will not compile if it is not found.
Call the Web Service interface
Create a New Web project in Eclipse, configure the Tomcat service, and copy the Java files from the first step into the project’s SRC directory.
Create your own class in the SRC directory as follows:
package demo;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import cn.com.webxml.ArrayOfString;
import cn.com.webxml.WeatherWS;
import cn.com.webxml.WeatherWSSoap;
@WebServlet(name = "ToolServlet", urlPatterns = "/Tool.do")
public class Select extends HttpServlet{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
String func = request.getParameter("func");
String str = request.getParameter("str");
switch(func){
case "weather" :
response.getWriter().write(weather(str));
break; } } public String weather(String str) { WeatherWS factory = new WeatherWS(); // Create a WeatherWS factory WeatherWSSoap WeatherWSSoap = factory.getweatherwssoap (); ArrayOfString weatherInfo = WeatherWSSoap. GetWeather (STR, null); ArrayOfString weatherInfo = WeatherWSSoap. List<String> lstWeatherInfo = weatherInfo.getString(); String weather_str = lstWeatherInfo.get(4); // Return one of the weather messagesreturnweather_str; }}Copy the code
Front-end JSP page, JS code into the following:
<script type="text/javascript">...function f_weather(){
s_weather = document.getElementById("weather").value;
t_code = document.getElementById("t-code").value;
if (y_code == t_code){
$.ajax({
url:"<%=application.getContextPath()%>/Tool.do",
data:{
"func":"weather"."str":s_weather
},
Type:"post",
success:function(res){
document.getElementById('r-main').innerHTML = res;
},
error:function(err){ alert(err); }})}else{
alert("Verification code is not correct");
}
}
</script>
Copy the code
Note: Some problems encountered when calling the Web Service interface.
Garbled characters occur when Chinese parameters are passed between JSP pages and Java classes
(1) Set the project encoding to UTF-8 in Eclipse, refer to Eclipse to set the encoding format to UTF-8
URIEncoding=” utF-8 “;
<Connector connectionTimeout="20000" port="8888" protocol="HTTP / 1.1" redirectPort="8443" URIEncoding="UTF-8"/>
Copy the code
(3) Add Response. setContentType(“text/ HTML; charset=UTF-8”);
An error occurred while calling the method provided by the Web Service interface that the parameter object was not instantiated
For example, the Web Service interface to query train information changes the method’s second argument, NULL, to an empty string
public String train(String str) {
TrainTimeWebService factory = new TrainTimeWebService();
TrainTimeWebServiceSoap trainTimeWebServiceSoap = factory.getTrainTimeWebServiceSoap();
//ArrayOfString trainInfo = trainTimeWebServiceSoap.getStationAndTimeByTrainCode(str, null);
ArrayOfString trainInfo = trainTimeWebServiceSoap.getStationAndTimeByTrainCode(str, "");
List<String> lsttrainInfo = trainInfo.getString();
return res;
}
Copy the code