cxf+Spring的webservice应用

服务端的开发:

SEI:

package com.xh.ws.sei;

import javax.jws.WebService;

import com.xh.ws.bean.User;

@WebService
public class SayHelloImpl implements SayHello {

	public SayHelloImpl() {
		super();
		System.out.println("調用....SayHelloImpl()");
	}

	@Override
	public User sayHello(User u) {
		System.out.println("server:sayhello>>>>"+u.toString());
		return u;
	}

}

bean:

package com.xh.ws.bean;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "User")
public class User {

	private int id;
	private String name;
	private String password;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public User()
	{

	}

	public User(int id,String name,String password)
	{
		this.id=id;
		this.name=name;
		this.password=password;

	}

	public String toString()
	{
		return ">>>"+id+":"+name+":"+password;
	}

}

Test:

package com.xh.ws.test;

public class TestServer {

	public static void main(String[] args) {
		/**
		 * 未用sprig之前的方法
			Endpoint.publish("http://127.0.0.1:34512", new SayHelloImpl());
			System.out.println("发布成功!");
		 */

		/**
		 * 使用spring后,只需要部署在TOMcat就可以了
		 */

	}
}

bean.xml:

<?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">
   <import resource="classpath:META-INF/cxf/cxf.xml" /> 

   <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
   <jaxws:endpoint
     id="UserProcess"
     implementor="com.xh.ws.sei.SayHelloImpl"
     address="/userProcess" />
</beans>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
	<display-name>hello1</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>

	</welcome-file-list>

	<context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:bean.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>/*</url-pattern>
   </servlet-mapping>

</web-app>

客户端:

bean.xml:

<?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">
<jaxws:client id="UserClient" serviceClass="com.xh.ws.sei.SayHello"
                  address="http://localhost:8080/ws_spring_s/userProcess"
                  />
</beans>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
	<display-name>hello1</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>

	</welcome-file-list>

	<context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:bean.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>/*</url-pattern>
   </servlet-mapping>

</web-app>

Test:

package com.xh.ws.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xh.ws.sei.SayHello;
import com.xh.ws.sei.User;

public class TestClient {

	public static void main(String[] args) {
		/**
		 * 未用sprig之前的方法
			SayHelloImplService factory=new SayHelloImplService();
			System.out.println(factory.getSayHelloImplPort().sayHello("lina"));
		 *
		 */

		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:bean.xml");
		SayHello sayHello=(SayHello) applicationContext.getBean("UserClient");
		User u=new User(12,"lina","123123");
		System.out.println(sayHello.sayHello(u).toString());
	}
}
时间: 2024-10-21 23:38:45

cxf+Spring的webservice应用的相关文章

cxf+Spring的webservice应用--拦截器

接上篇:cxf+Spring的webservice应用 为客户端添加出拦截器: package com.xh.ws.interceptor; import java.util.List; import javax.xml.namespace.QName; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.headers.Header; import org.apache.cxf.helpers.DOMUti

CXF Spring 实现WebService - 观千剑而后识器,操千曲而后晓声。 - 博客频道

来源:http://blog.csdn.net/shimiso/article/details/17586341#1536434-hi-1-76518-42d97150898b1af15ddaae52f91f09c2

cxf-CXF+Spring做webservice 部署在was服务器上报错

问题描述 CXF+Spring做webservice 部署在was服务器上报错 Error 500: javax.servlet.ServletException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'handService': Invocation of init method failed; nested exception is javax.xml.w

cxf 3.1.5 spring 发布webservice ,不打印客户端的请求日志

问题描述 cxf 3.1.5 spring 发布webservice ,不打印客户端的请求日志 cxf 3.1.5 spring 发布webservice ,不打印客户端的请求日志, jaxws:endpoint 发布方式,怎么配置?非常感谢

如何将用cxf开发的Webservice发布到tomcat不使用spring

问题描述 如何将用cxf开发的Webservice发布到tomcat不使用spring 如何将用cxf开发的Webservice发布到tomcat不使用spring 解决方案 把配置文件和class放到web-inf下启动tomcat即可

cxf spring @Autowired注入null

问题描述 cxf spring @Autowired注入null 配置文件 <!-- 配置文件资源导入 --> <import resource=""classpath:META-INF/cxf/cxf.xml""/> <import resource=""classpath:META-INF/cxf/cxf-extension-soap.xml""/> <import resourc

求一套简单的cxf+spring restful的源码

问题描述 求一套简单的cxf+spring restful的源码 能够通过简单的html get方式访问并传值就可以了 接口用@path定义,一经采纳立马给悬赏,可追加!急,在线等,谢谢了 解决方案 试试以下例子 cxf.spring集成webservice 配置restful方式 刚刚配置完spring集成cxf使用restful方式部署webservice,整个过程感觉就是爽和简单,欢迎抛砖引玉 第一步:当然是下载jar包了 使用到的jar有以下: 1.spring jar包我就不说了,地球

cxf spring集合中开发Web Service遇见的java.util.Date无缺省函数问题

问题描述 cxf spring集合中开发Web Service遇见的java.util.Date无缺省函数问题 在domain中有User类public class User {          private String userName;private String userPassword;private String mobile;private String email;private String nickname;private String region;private ja

用cxf写的webservice在调用时出现以下错误求解答,求帮助

问题描述 用cxf写的webservice在调用时出现以下错误求解答,求帮助 Unmarshalling Error: 意外的元素 (uri:"http://hui.cxf.www/", local:"say").所需元素为<{}say> 解决方案 你的xml或者json不标准,检查下特殊符号有没有转义,有没有获取完全,编码是否正确等.