用cxf开发一个可以被安卓调用的接口
生活随笔
收集整理的這篇文章主要介紹了
用cxf开发一个可以被安卓调用的接口
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.開發service接口層
package com.yun.service.User;import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;import com.yun.bean.User;@WebService
@SOAPBinding(style=Style.RPC)
public interface UserService {//public String add(String user);
// @WebMethod(operationName="sayHi")public String sayHi(@WebParam(name = "userName")String userName);public User add(@WebParam(name = "user")User user);public String getUser();
}
package com.yun.service.User.impl;import javax.jws.WebService;import org.springframework.stereotype.Component;import com.yun.bean.User;
import com.yun.service.User.UserService;
//@WebService(targetNamespace="http://User.service.yun.com/")
//@Component
@WebService(endpointInterface = "com.yun.service.User.UserService")
public class UserServiceImpl implements UserService {public String sayHi(String userName) {// TODO Auto-generated method stubSystem.out.println("hello,"+userName);return "hello,"+userName;}public User add(User user) {// TODO Auto-generated method stubreturn user;}public String getUser() {// TODO Auto-generated method stubreturn null;}
}
<jaxws:endpoint id="user"implementor="com.yun.service.User.impl.UserServiceImpl" address="/UserService"publish="true" />
2.開發測試代碼
<strong><span style="font-size:18px;">package com.yun.test;import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE;import com.yun.bean.User;public class TestAndroid {public static void main(String[] args) {//targetNamespace="http://User.service.yun.com/"final String SERVICE_NS = "http://User.service.yun.com/";final String SERVICE_URL = "http://localhost:8080/WebService/webservice/UserService";//調用的方法String methodName = "sayHi";//創建httpTransportSE傳輸對象HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);ht.debug = true;//使用soap1.1協議創建Envelop對象SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//實例化SoapObject對象SoapObject request = new SoapObject(SERVICE_NS, methodName);/*** 設置參數,參數名不一定需要跟調用的服務器端的參數名相同,只需要對應的順序相同即可* */request.addProperty("userName", "劉云生");//將SoapObject對象設置為SoapSerializationEnvelope對象的傳出SOAP消息envelope.bodyOut = request;try{//調用webServiceht.call(null, envelope);//txt1.setText("看看"+envelope.getResponse());if(envelope.getResponse() != null){System.out.println("有返回");SoapObject result = (SoapObject) envelope.bodyIn;String name = result.getProperty(0).toString();System.out.println("返回值 = "+name);}else{System.out.println("無返回");}}catch (Exception e) {e.printStackTrace();System.out.println(e.getMessage());}}} </span></strong>
3.測試結果
有返回 返回值 = hello,劉云生總結
以上是生活随笔為你收集整理的用cxf开发一个可以被安卓调用的接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高中学生计算机软件,基于智慧课堂的高中生
- 下一篇: 晶体塑性有限元 Abaqus 三维泰森多