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());
	}
}




目录
相关文章
|
1月前
|
监控 Java 数据处理
【Spring云原生】Spring Batch:海量数据高并发任务处理!数据处理纵享新丝滑!事务管理机制+并行处理+实例应用讲解
【Spring云原生】Spring Batch:海量数据高并发任务处理!数据处理纵享新丝滑!事务管理机制+并行处理+实例应用讲解
|
2月前
|
XML Java 数据格式
Spring 应用上下文探秘:生命周期解析与最佳实践
Spring 应用上下文探秘:生命周期解析与最佳实践
74 0
|
2月前
|
存储 Java Maven
QR码应用实战:Spring Boot与ZXing完美结合
QR码应用实战:Spring Boot与ZXing完美结合
29 0
|
2月前
|
消息中间件 存储 监控
搭建消息时光机:深入探究RabbitMQ_recent_history_exchange在Spring Boot中的应用【RabbitMQ实战 二】
搭建消息时光机:深入探究RabbitMQ_recent_history_exchange在Spring Boot中的应用【RabbitMQ实战 二】
32 1
|
27天前
|
安全 Java 数据安全/隐私保护
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
61 1
|
2月前
|
消息中间件 NoSQL Java
Redis Streams在Spring Boot中的应用:构建可靠的消息队列解决方案【redis实战 二】
Redis Streams在Spring Boot中的应用:构建可靠的消息队列解决方案【redis实战 二】
210 1
|
2月前
|
缓存 Java 数据库
优化您的Spring应用程序:缓存注解的精要指南
优化您的Spring应用程序:缓存注解的精要指南
45 0
|
22天前
|
存储 安全 Java
Spring Security应用讲解(Java案列演示)
Spring Security应用讲解(Java案列演示)
|
27天前
|
Prometheus 监控 Cloud Native
Spring Boot 应用可视化监控
Spring Boot 应用可视化监控
15 0
|
1月前
|
Cloud Native Java 开发者
Spring Boot 4.0:构建云原生Java应用的前沿工具
Spring Boot 4.0:构建云原生Java应用的前沿工具