天天看点

第一个webservice

第一个接口测试

参考来源来自:https://blog.csdn.net/qazwsxpcm/article/details/70370490

可能需要的需要的jar:

activation.jar

axis.jar

xmlsec-1.4.0.jar

….

第一个webservice

参考代码:

1:……………….开始编写一个接口…………………….

package acom;

import java.util.HashMap;

import java.util.Map;

import javax.jws.WebMethod;

import javax.jws.WebService;

import javax.xml.ws.Endpoint;

import com.google.gson.Gson;

import com.google.gson.GsonBuilder;

import net.sf.json.JSONObject;

@WebService

public class JwsKserviceHello {

/** 供客户端调用方法  该方法是非静态的,会被发布
 * @param name  传入参数
 * @return String 返回结果
 * */
public String getValue(String name){
    return "欢迎你! "+name;
}

/***
 * 封装成json 格式 1
 * json-lib是一个比较老的解决方案,近几年都没有升级过,它的适用环境是JDK1.5,使用JDK1.6就有可能会报错。所以配置上加入classifier-jdk15来解决这个问题
 * @param name
 * @return
 */
public String getjson1(String name){
    Map map = new HashMap();

    if(name!=null && !"".equals(name)){
        map.put("msg", "yes");
    }else{
        map.put("msg", "no");
    }
    map.put("name", name);
    JSONObject jsonObject = JSONObject.fromObject(map);
    String result = jsonObject.toString();
    System.out.println("输出的结果是:json对象转化" + jsonObject);
    System.out.println("输出的结果是:jsonjson字符串" +result);
    return result;
}

/**
 *封装成json 格式 2 
 * google
 * @param name
 * @return
 */
public String getjson2(String name){
    Map map = new HashMap();
    Gson gson = new GsonBuilder().create();
    if(name!=null && !"".equals(name)){
        map.put("msg", "yes");
    }else{
        map.put("msg", "no");
    }
    map.put("name", name);
    String result = gson.toJson(map);
    System.out.println("输出的结果是:jsonjson2字符串" +result);
    return result;
}


/**
 * 方法上加@WebMentod(exclude=true)后,此方法不被发布;
 * @param name
 * @return
 */
@WebMethod(exclude=true)  
public String getHello(String name){
    return "你好! "+name;
}

/** 静态方法不会被发布
 * @param name
 * @return
 */
public static String getString(String name){
    return "再见!"+name;
}


 //通过EndPoint(端点服务)发布一个WebService
public static void main(String[] args) {
 /*参数:1,本地的服务地址;
       2,提供服务的类;
  */
 Endpoint.publish("http://192.168.2.7:8080/Service/KserviceHello", new JwsKserviceHello());
 System.out.println("发布成功!");
 //发布成功后 在浏览器输入 http://192.168.2.7:8080/Service/KserviceHello?wsdl
}
           

}

……………….结束编写一个接口…………………….

:2.………………开始由命令生成接口…………………….*

打开电脑

在dos命令下输入 wsimport -s “src目录” -p “生成类所在包名” -keep “wsdl发布地址”

例如本例子

wsimport -s D:\workServiceText\TextService\src -p com.acom -keep http://192.168.2.7:8080/Service/KserviceHello?wsdl

第一个webservice

:2.………………结束由命令生成接口…………………….*

:3.………………开始新建一个class 去调用接口…………………….*

package com.acom;

import com.acom.JwsKserviceHelloService;

public class JwsKClientHello {

public static void main(String[] args) {
    //调用webservice
    JwsKserviceHello hello=(JwsKserviceHello)new JwsKserviceHelloService().getJwsKserviceHelloPort();
    String name=hello.getValue("panchengming");
    String json1=hello.getjson2("panchengming");
    String json2=hello.getjson2("");
    System.out.println(name);
    System.out.println(json1);
    System.out.println(json2);
           

}

}

:3.………………结束新建一个class 去调用接口…………………….*

4.注意事项 lib 中很多.jar 本身没有 ,由于调用2中 命令生成接口 ,自动加载进来

第一个webservice
第一个webservice

继续阅读