CXF 시작

서버와 클라이언트는 모두 Java Project입니다. 먼저 서버 코드를 보십시오
일단 세이.
package com.jadyer.service;

import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import com.jadyer.model.User;

@WebService(targetNamespace="http://blog.csdn.net/jadyer")
public interface HelloService {
	@WebResult(name="sayHelloResult")
	public String sayHello(@WebParam(name="name")String name);
	
	/**
	 *            
	 */
	public User getUserByID(int id);
	
	/**
	 *     jadyer           ,               
	 */
	public List<User> getUserList(User user);
}

다음은 SIB.
package com.jadyer.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import com.jadyer.model.User;

@WebService(endpointInterface="com.jadyer.service.HelloService", targetNamespace="http://blog.csdn.net/jadyer")
public class HelloServiceImpl implements HelloService {
	//          
	private static Map<Integer, User> userMap = new HashMap<Integer, User>();
	static{
		userMap.put(1, new User(1, "admin", "xuanyu", 11));
		userMap.put(2, new User(2, "jadyer", "hongyu", 22));
	}
	
	@Override
	public String sayHello(String name) {
		System.out.println("Receive the name=[" + name + "]");
		if(null==name){
			return "Hello,World";
		}else{
			return "Hello," + name;
		}
	}

	@Override
	public User getUserByID(int id) {
		return userMap.get(id);
	}
	
	@Override
	public List<User> getUserList(User user) {
		List<User> userList = new ArrayList<User>();
		if(user.getUsername().equals("jadyer")){
			for(Map.Entry<Integer, User> entry : userMap.entrySet()){
				userList.add(entry.getValue());
			}
		}else{
			userList.add(user);
		}
		return userList;
	}
}

다음은 사용된 솔리드 클래스 User입니다.java
package com.jadyer.model;

public class User {
	private int id;
	private String username;
	private String password;
	private int age;
	
	/*     setter getter */
	
	public User(){}
	public User(int id, String username, String password, int age) {
		this.id = id;
		this.username = username;
		this.password = password;
		this.age = age;
	}
}

다음은 SOAPHeader 정보를 받기 위해 서버에서 사용자 정의한Handler 클래스입니다
package com.jadyer.handler;

import java.util.Iterator;
import java.util.Set;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

public class LicenseHandler implements SOAPHandler<SOAPMessageContext> {
	@Override
	public Set<QName> getHeaders() {
		return null;
	}

	@Override
	public void close(MessageContext context) {}

	@Override
	public boolean handleFault(SOAPMessageContext context) {
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean handleMessage(SOAPMessageContext context) {
		Boolean isOutBound = (Boolean)context.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY);
		if(!isOutBound){
			SOAPHeader header = null;
			try {
				SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
				header = envelope.getHeader();
				if(null == header){
					header = envelope.addHeader();
				}
			} catch (SOAPException e) {
				e.printStackTrace();
			}
			Iterator<SOAPHeaderElement> iterator = header.getChildElements();
			while(iterator.hasNext()){
				SOAPHeaderElement headerElement = iterator.next();
				if(headerElement.getLocalName().equals("licenseInfo")){
					System.out.println("Receive the header=[" + headerElement.getTextContent() + "]");
				}
			}
		}
		return true;
	}
}

마지막으로 서버에서 서비스를 발표하는 데 사용되는 Server App.java
package com.jadyer.server;

import java.util.ArrayList;
import java.util.List;

import javax.xml.ws.handler.Handler;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import com.jadyer.handler.LicenseHandler;
import com.jadyer.service.HelloService;
import com.jadyer.service.HelloServiceImpl;

public class ServerApp {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		//  JAX-WS     
		//Endpoint.publish("http://127.0.0.1:8088/myHelloService", new HelloServiceImpl());
		
		//  CXF     
		JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
		factory.setAddress("http://127.0.0.1:8088/myHelloService");
		factory.setServiceClass(HelloService.class);
		factory.setServiceBean(new HelloServiceImpl());
		
		//        SOAP    ,        tcpmon 
		factory.getInInterceptors().add(new LoggingInInterceptor());
		factory.getOutInterceptors().add(new LoggingOutInterceptor());
		
		//set Handler(    ,  SIB  @HandlerChain,     xml  )
		//  CXF  , Handler        ,         Interceptor   Handler
		List<Handler> handlers = new ArrayList<Handler>();
		handlers.add(new LicenseHandler());
		factory.setHandlers(handlers);
		
		factory.create();
	}
}

OK, 서버 코드 예제 완료, 다음은 클라이언트 코드
우선 클라이언트가 SOAPHeader 정보를 보내는 데 사용하는Handler 클래스를 사용자 정의합니다.
package com.jadyer.handler;

import java.util.Set;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

public class LicenseHandler implements SOAPHandler<SOAPMessageContext> {
	@Override
	public Set<QName> getHeaders() {
		return null;
	}

	@Override
	public void close(MessageContext context) {}

	@Override
	public boolean handleFault(SOAPMessageContext context) {
		return false;
	}

	@Override
	public boolean handleMessage(SOAPMessageContext context) {
		Boolean isOutBound = (Boolean)context.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY);
		if(isOutBound){
			try {
				SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
				SOAPHeader header = envelope.getHeader();
				if(null == header){
					header = envelope.addHeader();
				}
				QName qname = new QName("http://blog.csdn.net/jadyer", "licenseInfo", "ns");
				header.addHeaderElement(qname).setValue("Jadyer");
				return true;
			} catch (SOAPException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
}

마지막으로 클라이언트가 서버의 아날로그 입구 클라이언트를 호출합니다.java
참고 구체적인 클라이언트 코드는 CXF에서 제공하는 wsdj2java에서 생성
package com.jadyer.client;

import java.util.ArrayList;
import java.util.List;

import javax.xml.ws.handler.Handler;

import net.csdn.blog.jadyer.HelloService;
import net.csdn.blog.jadyer.User;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.jadyer.handler.LicenseHandler;

/**
 *   CXF   wsdj2java
 * 1) D:\Develop\apache-cxf-2.7.0\bin      path 
 * 2)  CXF   wsdl2java.bat       ,    JAX-WS wsimport    
 *   wsdl2java -d D:/Download/ -frontend jaxws21 -keep -verbose http://127.0.0.1:8088/myHelloService?wsdl
 *   :wsimport   -->http://blog.csdn.net/jadyer/article/details/8692108
 * @create May 30, 2013 5:09:13 PM
 * @author   <http://blog.csdn.net/jadyer>
 */
public class ClientApp {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		/**
		 *   JAX-WS        
		 */
		//System.out.println(new HelloServiceService().getHelloServicePort().sayHello("  "));
		/**
		 *   CXF        
		 */
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setAddress("http://127.0.0.1:8088/myHelloService");
		factory.setServiceClass(HelloService.class);
		factory.getInInterceptors().add(new LoggingInInterceptor());
		factory.getOutInterceptors().add(new LoggingOutInterceptor());
		List<Handler> handlers = new ArrayList<Handler>();
		handlers.add(new LicenseHandler());
		factory.setHandlers(handlers);
		HelloService hello = (HelloService)factory.create();
		//System.out.println(hello.sayHello("  "));
		//System.out.println(ReflectionToStringBuilder.toString(hello.getUserByID(2), ToStringStyle.MULTI_LINE_STYLE));
		User user = new User();
		user.setUsername("jadyer");
		List<User> userList = hello.getUserList(user);
		for(User uu : userList){
			System.out.println(ReflectionToStringBuilder.toString(uu, ToStringStyle.MULTI_LINE_STYLE));
		}
	}
}

마지막으로 콘솔 출력을 붙여볼게요.
다음은 서버의 컨트롤러 출력입니다
2013-5-31 22:28:59 org.apache.cxf.services.HelloServiceService.HelloServicePort.HelloService
  : Inbound Message
----------------------------
ID: 4
Address: http://127.0.0.1:8088/myHelloService
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[351], content-type=[text/xml; charset=UTF-8], Host=[127.0.0.1:8088], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache CXF 2.7.0]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><ns:licenseInfo xmlns:ns="http://blog.csdn.net/jadyer">Jadyer</ns:licenseInfo></soap:Header><soap:Body><ns2:getUserList xmlns:ns2="http://blog.csdn.net/jadyer"><arg0><age>0</age><id>0</id><username>jadyer</username></arg0></ns2:getUserList></soap:Body></soap:Envelope>
--------------------------------------
Receive the header=[Jadyer]
2013-5-31 22:28:59 org.apache.cxf.services.HelloServiceService.HelloServicePort.HelloService
  : Outbound Message
---------------------------
ID: 4
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header/><soap:Body><ns2:getUserListResponse xmlns:ns2="http://blog.csdn.net/jadyer"><return><age>11</age><id>1</id><password>xuanyu</password><username>admin</username></return><return><age>22</age><id>2</id><password>hongyu</password><username>jadyer</username></return></ns2:getUserListResponse></soap:Body></soap:Envelope>
--------------------------------------

다음은 클라이언트의 콘솔 출력입니다
2013-5-31 22:28:58 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
  : Creating Service {http://blog.csdn.net/jadyer}HelloServiceService from class net.csdn.blog.jadyer.HelloService
2013-5-31 22:28:59 org.apache.cxf.services.HelloServiceService.HelloServicePort.HelloService
  : Outbound Message
---------------------------
ID: 1
Address: http://127.0.0.1:8088/myHelloService
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><ns:licenseInfo xmlns:ns="http://blog.csdn.net/jadyer">Jadyer</ns:licenseInfo></soap:Header><soap:Body><ns2:getUserList xmlns:ns2="http://blog.csdn.net/jadyer"><arg0><age>0</age><id>0</id><username>jadyer</username></arg0></ns2:getUserList></soap:Body></soap:Envelope>
--------------------------------------
2013-5-31 22:28:59 org.apache.cxf.services.HelloServiceService.HelloServicePort.HelloService
  : Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {content-type=[text/xml;charset=UTF-8], Server=[Jetty(8.1.7.v20120910)], transfer-encoding=[chunked]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header/><soap:Body><ns2:getUserListResponse xmlns:ns2="http://blog.csdn.net/jadyer"><return><age>11</age><id>1</id><password>xuanyu</password><username>admin</username></return><return><age>22</age><id>2</id><password>hongyu</password><username>jadyer</username></return></ns2:getUserListResponse></soap:Body></soap:Envelope>
--------------------------------------
net.csdn.blog.jadyer.User@168497f6[
  age=11
  id=1
  password=xuanyu
  username=admin
]
net.csdn.blog.jadyer.User@27db0da1[
  age=22
  id=2
  password=hongyu
  username=jadyer
]

좋은 웹페이지 즐겨찾기