开发者社区> 蓝企鹅lo> 正文

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




版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
java WebService CXF Spring 自定义拦截器 附实例源码
java WebService CXF Spring 自定义拦截器 附实例源码
58 0
java WebService CXF Spring 自定义拦截器 附实例源码
java WebService CXF Spring 自定义拦截器 附实例源码
99 0
CXF+Spring+Hibernate实现RESTful webservice服务端实例
CXF+Spring+Hibernate实现RESTful webservice服务端实例
1429 0
+关注
文章
问答
文章排行榜
最热
最新
相关电子书
更多
电商网站需求分析和架构设计Spring Boot2.6入门
立即下载
云上Docker的Spring Cloud微服务应用实践分享
立即下载
Spring Boot 2.6.0电商网站开发实战
立即下载