webService实例:
service:
接口
public interface Business {
public String echo(String message);
}
接口实现
@WebService(name = "Business", serviceName = "BusinessService",
targetNamespace = "http://com.shangfi.javabase.webservice.webservice/client")
@SOAPBinding(style=SOAPBinding.Style.RPC)
public class BussinessImpl implements Business {
public String echo(String message) {
message="message demo";
return "Server response:" + message;
}
}
接口发发布
/**
* @author Administrator
* 自动生成 代码 wsimport -keep http://localhost:8080/ShingFi/com/shangfi/javabase/webservice.webservice?wsdl
*/
public class Server {
public static void main(String[] args) throws Exception {
Endpoint.publish(
"http://localhost:8080/ShingFi/com/shangfi/javabase/webservice.webservice",
new BussinessImpl());
System.out.println(" Server Start");
}
}
client:
用户命令来生成代码:wsimport -keep http://localhost:8080/ShingFi/com/shangfi/javabase/webservice.webservice?wsdl
生成如何
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.1.6 in JDK 6
* Generated source version: 2.1
*
*/
@WebService(name = "Business", targetNamespace = "http://com.shangfi.javabase.webservice.webservice/client")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface Business {
/**
*
* @param arg0
* @return
* returns java.lang.String
*/
@WebMethod
@WebResult(partName = "return")
public String echo(
@WebParam(name = "arg0", partName = "arg0")
String arg0);
}
**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.1.6 in JDK 6
* Generated source version: 2.1
*
*/
@WebServiceClient(name = "BusinessService", targetNamespace = "http://com.shangfi.javabase.webservice.webservice/client", wsdlLocation = "http://localhost:8080/ShingFi/com/shangfi/javabase/webservice.webservice?wsdl")
public class BusinessService
extends Service
{
private final static URL BUSINESSSERVICE_WSDL_LOCATION;
private final static Logger logger = Logger.getLogger(com.shangfi.javabase.webservice.webservice.client.BusinessService.class.getName());
static {
URL url = null;
try {
URL baseUrl;
baseUrl = com.shangfi.javabase.webservice.webservice.client.BusinessService.class.getResource(".");
url = new URL(baseUrl, "http://localhost:8080/ShingFi/com/shangfi/javabase/webservice.webservice?wsdl");
} catch (MalformedURLException e) {
logger.warning("Failed to create URL for the wsdl Location: 'http://localhost:8080/ShingFi/com/shangfi/javabase/webservice.webservice?wsdl', retrying as a local file");
logger.warning(e.getMessage());
}
BUSINESSSERVICE_WSDL_LOCATION = url;
}
public BusinessService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public BusinessService() {
super(BUSINESSSERVICE_WSDL_LOCATION, new QName("http://com.shangfi.javabase.webservice.webservice/client", "BusinessService"));
}
/**
*
* @return
* returns Business
*/
@WebEndpoint(name = "BusinessPort")
public Business getBusinessPort() {
return super.getPort(new QName("http://com.shangfi.javabase.webservice.webservice/client", "BusinessPort"), Business.class);
}
/**
*
* @param features
* A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* @return
* returns Business
*/
@WebEndpoint(name = "BusinessPort")
public Business getBusinessPort(WebServiceFeature... features) {
return super.getPort(new QName("http://com.shangfi.javabase.webservice.webservice/client", "BusinessPort"), Business.class, features);
}
}
client调用
public class Client {
public static void main(String[] args) throws Exception {
BusinessService businessService = new BusinessService();
Business business = businessService.getBusinessPort();
String message = business.echo("test");
System.out.println(" Client Start");
System.out.println(message);
}
}