用cxf开发一个可以被安卓调用的接口

本文详细介绍了如何开发RESTful风格的Web服务接口,并通过Android客户端进行调用,包括服务接口定义、实现和服务调用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 stub
		System.out.println("hello,"+userName);
		return "hello,"+userName;
	}

	public User add(User user) {
		// TODO Auto-generated method stub
		return user;
	}

	public String getUser() {
		// TODO Auto-generated method stub
		
		
		return 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{
            //调用webService
            ht.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,刘云生







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liuyunshengsir

微信:lys20191020

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值