前端之家收集整理的这篇文章主要介绍了
webservice,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package webservce;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class ServceTest { public String getMessage(String name) { return name+"你好"; } public static void main(String[] args) { Endpoint.publish("http://localhost:8080/MyService",new ServceTest());//发布服务 System.out.println("ServiceTestStart"); } }
-
客户端
E:\eclipse\workspace\Advance\src 为项目路径,webservceclient 为项目下的包。cmd执行命令后,客户端相关java代码生成在E:\eclipse\workspace\Advance\src\webservceclient目录下。特别注意执行该命令把服务起来,否则会报 Failed.noservice=在提供的 WSDL 中找不到 wsdl:service:。出现正在生成代码…正在编译代码…成功。
wsimport -s E:\\eclipse\\workspace\\Advance\\src -p webservceclient -keep http://localhost:8080/MyService?wsdl
-
测试
我测试和服务端写同一目录下了,注意ServceTest是接口。打印出”sinaihalo你好”,测试成功。
package webservce;
import webservceclient.ServceTestService;
public class ClientTest { public static void main(String[] args) { webservceclient.ServceTest serviceTest = new ServceTestService().getServceTestPort();//初始化对象 String name = serviceTest.getMessage("sinaihalo");//调用服务端方法 System.out.println(name);//打印返回结果 } }
-
httpclient方式测试
首先导httpclient的jar包,这种方式自己拼请求,运行结果:
<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:getMessageResponse xmlns:ns2="http://webservce/"><return>sinaihalo你好</return></ns2:getMessageResponse></S:Body></S:Envelope>
package com.ultrapower.nettech.obm.server;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public class TestWebService { public static void main(String[] args) throws Exception { Map<String,String> map = new HashMap<String,String>(); //拼接xml请求,带有请求头 String soapRequestData = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservce/\">"
+"<soapenv:Header/>"
+"<soapenv:Body>"
+"<web:getMessage>"
+" <!--Optional:-->"
+"<arg0>sinaihalo</arg0>"
+"</web:getMessage>"
+"</soapenv:Body>"
+"</soapenv:Envelope>";
try {
String method = "http://localhost:8080/MyService";//比如http://192.177.222.222:8888/services/Service_Name/Function_Name
PostMethod postMethod = new PostMethod(method);
byte[] b = soapRequestData.getBytes("utf-8");
InputStream is = new ByteArrayInputStream(b,0,b.length);
RequestEntity re = new InputStreamRequestEntity(is,b.length,"text/xml; charset=utf-8");
postMethod.setRequestEntity(re);
HttpClient httpClient = new HttpClient();
int statusCode = httpClient.executeMethod(postMethod);
//200说明正常返回数据
if (statusCode != 200) {
//internet error
System.out.println(statusCode);
}
soapRequestData = postMethod.getResponseBodyAsString();
System.out.println(soapRequestData);
} catch (Exception e) {
e.printStackTrace();
}
}
}
原文链接:/webservice/832718.html