使用MyEclipse创建WebService简单服务端和客户端
1.创建 WebService 简单服务端
1).新建一个 Web Service Project
File---New--- Web Service Project
2).新建一个普通服务方法
package cn.com.sl.service;
public class FirstService {
public String Res(String in){
return "Oh "+in+" is OK!";
}
}
3).新建相应的 Web Service 接口
项目名---New--- other---Web Service---Framework:JAX-WS
---Strategy:Create web service from Java calss(Bottom-up scenario)
---Java class 选择需要提供的方法类名 ---Finsh
4).在WebRoot/WEB-INF/web.xml 中获取url端口名称
<servlet-mapping>如上“/FirstServicePort”即为“FirstServiceService”的端口名
<servlet-name>FirstServiceService</servlet-name>
<url-pattern>/FirstServicePort</url-pattern>
</servlet-mapping>
5).将服务端应用部署,进行测试
部署服务端,在浏览器中输入http://ip:port/projetname/port?wsdl,实例如“http://localhost:8080/WebserviceSecond/FirstServicePort?wsdl”,
即可展示部署的WebService。
2.创建 WebService 简单客户端
1).新建一个web project
File---New--- Web Project
2)新建一个Web Service Client
项目名---项目名---New--- other---Web Service Client---Framework:JAX-WS
---WSDL URL: 开放的WebService服务端的地址---过完流程,生成客户端的方法
注意:Service Definition中的两种方式WSDL File和WSDL URL。
WSDL File方式:是在浏览器中打开服务器端路径展示方法信息,右键另存为wsdl文件,在“WSDL File”后输入此文件的路径引入生成webservice方法;
WSDL URL方式:是在“WSDL URL”后直接数据服务端开放的地址来引入生成webservice方法。
一般不建议使用WSDL File方式,因为要注意wsdl文件的管理,会稍麻烦。
3).一般生成的客户端方法中有调用WebService服务的方式实例。通常在Service的类中。
如下
package cn.com.sl.service;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
/**
* This class was generated by the JAX-WS RI. JAX-WS RI 2.1.3-hudson-390-
* Generated source version: 2.0
* <p>
* An example of how this class may be used:
*
* <pre>
* 调用WebService服务的方法示例
* FirstServiceService service = new FirstServiceService();
* FirstServiceDelegate portType = service.getFirstServicePort();
* portType.res(...);
* </pre>
*
* </p>
*
*/
@WebServiceClient(name = "FirstServiceService", targetNamespace = "http://service.sl.com.cn/", wsdlLocation = "http://localhost:8080/WebserviceSecond/FirstServicePort?wsdl")
public class FirstServiceService extends Service {
private final static URL FIRSTSERVICESERVICE_WSDL_LOCATION;
private final static Logger logger = Logger
.getLogger(cn.com.sl.service.FirstServiceService.class.getName());
static {
URL url = null;
try {
URL baseUrl;
baseUrl = cn.com.sl.service.FirstServiceService.class
.getResource(".");
url = new URL(baseUrl,
"http://localhost:8080/WebserviceSecond/FirstServicePort?wsdl");
} catch (MalformedURLException e) {
logger
.warning("Failed to create URL for the wsdl Location: 'http://localhost:8080/WebserviceSecond/FirstServicePort?wsdl', retrying as a local file");
logger.warning(e.getMessage());
}
FIRSTSERVICESERVICE_WSDL_LOCATION = url;
}
public FirstServiceService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public FirstServiceService() {
super(FIRSTSERVICESERVICE_WSDL_LOCATION, new QName(
"http://service.sl.com.cn/", "FirstServiceService"));
}
/**
*
* @return returns FirstServiceDelegate
*/
@WebEndpoint(name = "FirstServicePort")
public FirstServiceDelegate getFirstServicePort() {
return super.getPort(new QName("http://service.sl.com.cn/",
"FirstServicePort"), FirstServiceDelegate.class);
}
}