iphone开发笔记四 webservice环境配置cxf使用

因为要与服务器交互。所以采用webservice的方法。大概查阅了一些介绍,现在大概分为axis2,cxf  

axis2可以支持多语言,而cxf只能用在java上面,但是cxf与spring结合的比较好。因为还有一套android开发也要用到服务器tomcat后台。所以共用一个webservice。

android采用hession调用后台服务。这里只介绍一下iphone调用webservice的方法。

后台用ssh框架也可以。在其上在搭建一个webservice

1、下载cxf

http://cxf.apache.org/

2、配置与使用

http://cxf.apache.org/docs/writing-a-service-with-spring.html有个user guide 里面有个spring的简单例子

2.1 配置jar包导入

Open up your favorite IDE and create a new project. The first thing we need to do is add the necessary CXF dependencies to the project. You can find these dependencies in the CXF distribution in the lib directory.

commons-logging-1.1.1.jar
geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.7.1.jar (or Sun's JavaMail jar)
geronimo-servlet_3.0_spec-1.0.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.1.jar
jaxb-impl-2.1.13.jar
jaxws-api-2.1.jar
neethi-3.0.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
stax2-api-3.1.1.jar
wsdl4j-1.6.2.jar
woodstox-core-asl-4.0.8.jar
xmlschema-core-2.0.jar
xml-resolver-1.2.jar

The Spring jars:

aopalliance-1.0.jar
spring-core-3.0.5.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-web-3.0.5.RELEASE.jar

And the CXF jar:

cxf-2.4.0.jar

2.2 配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:applicationContext.xml</param-value>  
    </context-param>  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
    <servlet>  
        <servlet-name>CXFServlet</servlet-name>  
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>CXFServlet</servlet-name>  
        <url-pattern>/cxfService/*</url-pattern>  
    </servlet-mapping>  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
   

2.3配置spring xml

1212

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:jaxws="http://cxf.apache.org/jaxws"  
    xsi:schemaLocation="  
 http://www.springframework.org/schema/beans   
 http://www.springframework.org/schema/beans/spring-beans.xsd   
 http://cxf.apache.org/jaxws 
 http://cxf.apache.org/schemas/jaxws.xsd">  
   <!-- 上面  1、  http://cxf.apache.org/schemas/jaxws.xsd 
           2、 http://cxf.apache.org/jaxws  
           3、http://cxf.apache.org/schemas/jaxws.xsd
       为引入 CXF 的命名空间 -->   
    <import resource="classpath:META-INF/cxf/cxf.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
    <!-- 上面3个 import 为导入  CXF 的 Bean 定义文件-->
    
    
    <!-- 定义具体实现的 Bean ,这个 Bean 的定义与 Spring 普通的 Bean 定义是一样的 -->
    <!-- <bean id="hello" class="com.mycompany.service.impl.HelloWorldServiceImpl" />-->
    
    
   <!-- <jaxws:endpoint id="HelloWorldService" implementor="#hello" address="/HelloWorld" /> -->
 
  <jaxws:server id="HelloWorldService" address="/HelloWorld">
  <jaxws:serviceBean>
   <bean id="hello" class="com.mycompany.service.impl.HelloWorldServiceImpl" /> 
   <!-- <ref bean="hello" /> -->
  </jaxws:serviceBean>
 </jaxws:server>
  </beans>  



2.4 编辑webservice

package com.mycompany.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService
public interface HelloWorldService {
	 @WebMethod(operationName = "hello")//opertionName默认是方法名  
	 @WebResult(name="result")//返回值 不标注 默认为result
	 public String hello(); 
	 
	 @WebMethod(operationName = "helloParame")//opertionName默认是方法名  
	 @WebResult(name="result")//返回值 不标注 默认为result
	 public String helloParame(@WebParam(name="name")String name); //xml中参数名字定义
	 
}
//@WebService是必须的;@WebParam不是必须的。
//@WebParam(name="userName") 如果没有@WebParam的描述,在wsdl文件内描述的方法中,参数名将变为arg0,arg1…以此类推.


 

2.5 实现

package com.mycompany.service.impl;

import java.io.IOException;
import java.util.Calendar;

import javax.jws.WebService;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.mycompany.service.HelloWorldService;
@WebService
public class HelloWorldServiceImpl implements HelloWorldService {
	public String hello() {
		return "Hello "  + ", currentTime is " + Calendar.getInstance().getTime();   
	}
	public String helloParame(String name) {
		
		 String str="带参数为"+name+ "的webservice demo, currentTime is " + Calendar.getInstance().getTime();
		 ObjectMapper objectMapper = new ObjectMapper(); 
		 objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
		 String jackjsonstr = null;
		try {
			jackjsonstr = objectMapper.writeValueAsString(str);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		 System.out.println(str);
		 return jackjsonstr;
	}
}


 

2.6 测试代码

package com.mycompany.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.mycompany.service.HelloWorldService;

public class CxfWebserviceClientTest {

    public static void main(String[] args) {  
        //创建WebService客户端代理工厂  
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
        //注册WebService接口  
        factory.setServiceClass(HelloWorldService.class);  
        //设置WebService地址  
        factory.setAddress("http://localhost:9090/CxfWebserviceDemo/cxfService/HelloWorld");  
        HelloWorldService helloService = (HelloWorldService)factory.create();  
        System.out.println("invoke webservice...");  
        
        System.out.println("message context is:"+helloService.hello());
        System.out.println("message context is:"+helloService.helloParame("fax"));
    }  
}  


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值