cxf+Spring的webservice应用

服务端的开发:

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




上一篇:mysql 创建测试大表


下一篇:恭喜CocoStudio 1.5和Mac版本发布