CXF With Spring

Demo是以对象作为传输数据,Spring + CXF WebService HelloWorld

1、CXFServer

    1>编写接口:

package org.coder4j.ws.service;

import javax.jws.WebService;

import org.coder4j.ws.entity.User;

@WebService
public interface HelloWorld {
	User getUser();
}

    2>编写实现类:

package org.coder4j.ws.service.impl;

import javax.jws.WebService;

import org.coder4j.ws.entity.User;
import org.coder4j.ws.service.HelloWorld;

@WebService(endpointInterface = "org.coder4j.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

	@Override
	public User getUser() {
		User user = new User();
		user.setName("测试");
		user.setAge(7);
		user.setSex("男");
		return user;
	}
}

    3>使用到的实体类:

package org.coder4j.ws.entity;

import java.io.Serializable;

public class User implements Serializable {
	/**
	 * 用户实体
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	private Integer age;
	private String sex;

	public String getName() {
		return name;
	}

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

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

}

    4>web.xml配置信息:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	<display-name>cxf</display-name>
	<context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-cxf.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>/ws/*</url-pattern>
  </servlet-mapping>
</web-app>

    5>spring配置spring-cxf.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="helloWorld"
		implementor="org.coder4j.ws.service.impl.HelloWorldImpl" address="/HelloWorld" />
</beans>


2、CXFClient

    1>将CXFServer端的实体类和接口拷贝到项目中

    2>spring中bean的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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="helloClient" serviceClass="org.coder4j.ws.service.HelloWorld"
		address="http://127.0.0.1:8080/CXFServer/ws/HelloWorld" />
</beans>

    3>测试类:

package org.coder4j.ws;

import org.coder4j.ws.entity.User;
import org.coder4j.ws.service.HelloWorld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"spring-cxf.xml");
		HelloWorld helloService = (HelloWorld) context.getBean("helloClient");
		User user = (User) helloService.getUser();
		System.out.println(user);
	}
}




    注:CXF官网:http://cxf.apache.org/




你可能感兴趣的:(CXF,cxf教程,toturial)