您的位置:首页 > 编程语言 > Java开发

java 实现WebService 以及不同的调用方式

2016-08-04 15:31 253 查看
javaWebservice 简单了解,我们项目中用到javaWebservice为客户端提供接口,

接口类:

@WebService

public interface TestWs{

    @WebMethod(action = "getScSjRange")

    public String getScSjRange(String jsonStr);

}

接口实现类:

@WebService(endpointInterface = "com.tzinfo.app.test.TestWs", targetNamespace = "http://test.app.tzinfo.com/")//  http://+包名逆顺序
public class TestWsImpl implements TestWs{

  public String getScSjRange(String jsonStr){

         boolean success = true;

         String message = "";

         String errorCode = "";

         JSONObject jsonObject = JSONObject.fromObject(jsonStr);

         String nowTime = jsonObject.getString("nowTime");

         HhjService hhjScsjService = (HhjScsjService) AppContextUtil.getBean(HhjScsjService.class);

          List<String> list =null;

         try {

            list = hhjScsjService.getScsjRange(nowTime);

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

          JsonConfig cfg = new JsonConfig();

          cfg.setIgnoreTransientFields(true);

          cfg.addIgnoreFieldAnnotation(JsonlibIgore.class);

          return JSONUtilsExt.objects2json(cfg, "success", success, "message", message,"hhjScSj",list, "errorCode", errorCode);

    }

}

测试类:

TestClientXxx(){

    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();

    org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:9090/test/service/testWsImpl?wsdl");

      public void test_getScSjRange(){

        try {

            Object[] returnValueStrs = client.invoke("getScSjRange","{nowTime:'10:50:00'}");

            String returnJson = returnValueStrs == null ? null : (String) returnValueStrs[0];

            System.out.println(returnJson);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    public static void main(String[] args){  //有机运行即可测试

        TestClientXxxtc = new TestClientXxx();

        tc.test_getScSjRange();

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: