本人net程序员一枚,最近偶然间接到一个写接口的任务,仔细看了接口文档才发现,该任务分三个接口,两个提供的接口都是java写的,于是来了兴趣好好研究一波java的webservice。
进入正题:
JQ调用java webservice
由于这方面资料百度是在太少,于是只能慢慢研究,最终都是通过零星的方法综合起来,达到我要的目的。说明下我用的ide,我用的是IntelliJ IDEA,
1.首先创建java webservice 具体网上很多
这个就可以 :idea创建webservice
2.写webservice类
以下代码可能会有个别错误,主要是应为我只想留下结构和所需要的方法,我具体的类里面内容写的很多,应该是可以执行成功的,注释也是贴上来在补充的
package example;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebParam.Mode;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import java.text.SimpleDateFormat;
import java.util.Date;
@WebService()
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class CKInfoService
{//关于@等注解,我回在下面贴链接,也是我辛苦寻找来的东西呀
@WebMethod()// "获取查核任务"
public String getTask(@WebParam(mode = Mode.IN) String in0, @WebParam(mode = Mode.IN) String in1, @WebParam(mode = Mode.IN) String in2)
{//参数没有@WebParam(mode = Mode.IN)参数可能jq调用后参数值都为null,in 和out 我没具体研究 估计90%应该都是用in吧,大家可以看下面链接仔细研究
String result = "";
String code ="0000";
System.out.println("2接口参数0:" + in0 + " 1:" + in1 + " 2:" + in2);
String msg = "{\"message\":{\"task\":\"" + result + "\",\"info\":\"提示信息666\"},\"statusCode\":\"" + code + "\"}";
return msg;
}
}
贴上注解说明链接:@webservice说明,当然还有更详细的,我觉得这些应该是够用了。
还要说明的是,这个需要发布成webservice的类写好后按照上面连接的方法生成wsdl文件后,大致结构都是差不多的,
贴个wsdl结构连接吧:wsdl结构介绍
3.创建测试用html并引用jq
我的测试html 内容是这样的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<body>
首页
<button onclick="get()">点击</button>
<p id="pname"></p>
<p id="pmsg"></p>
</body>
<script type="text/javascript">
function get() {
var url = "http://localhost:8888/CKInfoService";
var method = 'getTask';
var pam0 = "arg0";
var pam1 = "arg1";
var pam2 = "arg2";
//jq想要调用java webservice 我目前就找到了这个方式 需要拼接成soap通信的结构,这个格式是固定的,下面我贴出我参考的文章连接
var p1 = '<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://example/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">' +
//'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'
'<soap:Body>' +
'<p:' + method + '>' +
//'<' + method + ' xmlns="http://example/">' +
'<' + pam0 + '>1</' + pam0 + '>' +
'<' + pam1 + '>1</' + pam1 + '>' +
'<' + pam2 + '>1</' + pam2 + '>' +
'</p:' + method + '>' +
//'</' + method + '>' +
'</soap:Body>' +
'</soap:Envelope>';
var p2 = '{"' + pam0 + '":"1","' + pam1 + '":"1","' + pam2 + '":"1"}';
var obj = ajax(p1, url);
if (!!obj) {
alert(obj);
$('#pmsg').text(obj);
}
}
function ajax(param, url) {
var res = null;
if (!param) {
return res;
}
$.ajax({
url: url,
data: param,
contentType: "text/xml",//jq格式一定是xml的
type: "POST",
async: false,
dataType: 'xml',//返回值的类型可以不设,建议设置成xml,应为返回值是xml格式的,方便成功返回后取值,这个类型设置不对,会导致请求错误进入到error方法里
success: function (data) {
if (!!data) {
res = data.getElementsByTagName("return")[0].innerHTML;
}
},
error: function (err) {
alert(err.responseText);
}
});
return res;
}
</script>
</html>
贴上请求数据拼接的参考的连接:拼接JQ请求的data
这里很容易遇到的错误就是后台获取不到参数,所有参数值都是null,按照我写的这个data格式应该是没有问题的,我是解决过的
网上还有另一种格式 两种很区别不大 唯一区别就是命名空间的声明那里。就是我构建请求data注释的那几行,对比就会发现差别,用注释的那种写法就会拿不到参数值。
好了 需要的我都贴出来了在贴一张结果图吧
这条消息就是后台输出的结果。
jq调用C#webservice
创建就没什么说的,网上关于C# webservice的内容一大堆,其实调用也不用我说也有很多 我还是小说一下吧
ajax这样写就可以,一般都是json格式上传和返回数据,不需要java那样麻烦
$.ajax({
contentType: "application/json",
url: "http//:localhost/Service/ImportBuildingService.asmx/GetViewDYH",
data: "{inparm:'" + 6666+ "'}", //GetViewDYH,是需要访问的webservice方法
type: "POST",
async: false,
dataType: 'xml',
success: function (data) {
if (!!data) {
alert(data)
}
},
error: function (err) {
alert(err.responseText);
}
});
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 创建WebService文件时下面一行是注释的,要jq能够访问到必须取消下面一行的注释
[System.Web.Script.Services.ScriptService]
public class ImportBuildingService: System.Web.Services.WebService
{
[WebMethod(EnableSession = false, Description = "服务")]
public string GetViewDYH(string inparm)
{
string a="{\"a\":\"6\"}";
return a;//返回的是xml格式//<string xmlns="http://tempuri.org/">{\"a\":\"6\"}</string>
//Context.Response.Write(a);//页面格式直接输出 {\"a\":\"6\"} 一般jq ajax数据交互使用这种方式
}
}
相对来说C#的webservice要方便操作很多 也许是我比较熟悉,如果各位有jq访问java webservice更方便的方法 请教给我 一定虚心学习!