웹 서비스 (5) Soap 프로 토 콜 에 대한 클 라 이언 트 와 서버 측의 실례 코드 (기초 와 진급)

20084 단어 webservice
서버 쪽 코드:
package org.sid.service;


import java.util.List;

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


@WebService(targetNamespace="http://www.sid.com/webservice")
public interface IMyService {
	@WebResult(name="add")
	public int add(@WebParam(name="a")int a,@WebParam(name="b")int b);

	@WebResult(name="user")
	public User addUser(@WebParam(name="user")User user);

	@WebResult(name="user")
	public User login(@WebParam(name="username")String username,
			@WebParam(name="password")String password) throws UserException;

	/**
	 *       
	 *                 ,   body   
	 * @param authInfo
	 * @return
	 */
	@WebResult(name="user")
	public List<User> list(@WebParam(header=true,name="authInfo")String authInfo);
}
package org.sid.service;

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

import javax.jws.HandlerChain;
import javax.jws.WebService;


@WebService(targetNamespace="http://www.sid.com/webservice",endpointInterface="org.sid.service.IMyService")
@HandlerChain(file="handler_chain.xml")
public class MyServiceImpl implements IMyService {
	private static List<User> users =  new ArrayList<User>();
	
	public MyServiceImpl(){
		users.add(new User(1, "admin", "   ", "admin"));
	}

	@Override
	public int add(int a, int b) {
		System.out.println("a+b="+(a+b));
		return a+b;
	}

	@Override
	public User addUser(User user) {
		users.add(user);
		return user;
	}

	@Override
	public User login(String username, String password) throws UserException{
		for (User user : users) {
			if (username.equals(user.getUsername())&&password.equals(user.getPassword())) {
				return user;
			}
		}
		throw new UserException("     ");
	}

	@Override
	public List<User> list(String authInfo) {
		System.out.println(authInfo);
		return users;
	}

}
package org.sid.service;

import javax.xml.ws.Endpoint;

public class MyServer {

	public static void main(String[] args) {
		Endpoint.publish("http://localhost:8989/ms", new MyServiceImpl());
	}
}
package org.sid.service;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class User {

	private int id;
	private String username;
	private String nickname;
	private String password;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getNickname() {
		return nickname;
	}
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public User(int id, String username, String nickname, String password) {
		super();
		this.id = id;
		this.username = username;
		this.nickname = nickname;
		this.password = password;
	}
	public User() {
		super();
	}
	
	
}
package org.sid.service;

public class UserException extends Exception {

	public UserException() {
		super();
	}

	public UserException(String arg0, Throwable arg1, boolean arg2, boolean arg3) {
		super(arg0, arg1, arg2, arg3);
	}

	public UserException(String arg0, Throwable arg1) {
		super(arg0, arg1);
	}

	public UserException(String arg0) {
		super(arg0);
	}

	public UserException(Throwable arg0) {
		super(arg0);
	}

}
package org.sid.test;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPFaultException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.junit.Test;
import org.sid.service.User;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * SAAJ
 *      client   ;     soap   ,          soap
 * Soap attachment api for java
 *
 */
public class TestSoap {
	String ns = "http://www.sid.com/webservice";
	String wsdlUrl = "http://localhost:8989/ms?wsdl";

	/**
	 * soap        
	 */
	@Test
	public void test01(){
		try {
			//1.      
			MessageFactory factory = MessageFactory.newInstance();
			//2.        soapMessage
			SOAPMessage message = factory.createMessage();
			//3.  SoapPart
			SOAPPart part = message.getSOAPPart();
			//4.  SOAPEnvelope
			SOAPEnvelope envelope = part.getEnvelope();
			//5.    soapEnvelope        body header   
			SOAPBody body = envelope.getBody();
			//6.  Qname       (Qname             )
			QName qname = new QName(ns, "add", "ns");
			body.addBodyElement(qname).setValue("123123");
			//             <>   < >
//			body.addBodyElement(qname).setValue("<a>2</a><a>2</a>");
			SOAPBodyElement ele = body.addBodyElement(qname);
			ele.addChildElement("a").setValue("11");
			ele.addChildElement("b").setValue("22");
			//    
			message.writeTo(System.out);
			
		} catch (SOAPException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * soap        
	 *    schema       
	 */
	@Test
	public void test02(){
		try {
			//1.    (Service)
			URL url = new URL(wsdlUrl);
			QName qname = new QName(ns, "MyServiceImplService");
			Service service = Service.create(url, qname);
			
			//2.  Dispatch
			//Service.Mode.MESSAGE  <>   
			Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns,"MyServiceImplPort"), 
					SOAPMessage.class, Service.Mode.MESSAGE);
			
			//3.  SOAPMessage
			SOAPMessage msg = MessageFactory.newInstance().createMessage();
			SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
			SOAPBody body = envelope.getBody();
			
			//4.  Qname           
			QName ename = new QName(ns,"add","nn");//<nn:add xmlns="xx">
			SOAPBodyElement ele = body.addBodyElement(ename);
			ele.addChildElement("a").setValue("22");
			ele.addChildElement("b").setValue("33");
			msg.writeTo(System.out);
			
			System.out.println("
invoking……"); //5. Dispatch SOAPMessage response = dispatch.invoke(msg); response.writeTo(System.out); // dom Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument(); String str = doc.getElementsByTagName("add").item(0).getTextContent(); System.out.println("
"+str); } catch (SOAPException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * payload * add */ @Test public void test03(){ try { //1. (Service) URL url = new URL(wsdlUrl); QName qname = new QName(ns, "MyServiceImplService"); Service service = Service.create(url, qname); //2. Dispatch, Source Dispatch<Source> dispatch = service.createDispatch(new QName(ns,"MyServiceImplPort"), Source.class, Service.Mode.PAYLOAD); //3. xml User user = new User(3,"zhangsan"," ","zhangsan"); JAXBContext ctx = JAXBContext.newInstance(User.class); Marshaller mar = ctx.createMarshaller(); StringWriter writer = new StringWriter(); // startDocument endDocument mar.setProperty(Marshaller.JAXB_FRAGMENT, true); mar.marshal(user, writer); //4. part addUser String payload = "<nn:addUser xmlns:nn=\""+ns+"\">"+writer.toString()+"</nn:addUser>"; System.out.println(payload); StreamSource rs = new StreamSource(new StringReader(payload)); //5. dispatch payload Source response = dispatch.invoke(rs); //6. Source DOM , Transform Transformer tran = TransformerFactory.newInstance().newTransformer(); DOMResult result = new DOMResult(); tran.transform(response, result); //7. XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nl = (NodeList) xPath.evaluate("//user", result.getNode(),XPathConstants.NODESET); System.out.println(nl.item(0).getNodeName()); // User ru = (User) ctx.createUnmarshaller().unmarshal(nl.item(0)); System.out.println(ru.getNickname()); } catch (IOException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (XPathExpressionException e) { e.printStackTrace(); } } /** * message list */ @Test public void test04(){ try { //1. (Service) URL url = new URL(wsdlUrl); QName qname = new QName(ns, "MyServiceImplService"); Service service = Service.create(url, qname); //2. Dispatch //Service.Mode.MESSAGE <> Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns,"MyServiceImplPort"), SOAPMessage.class, Service.Mode.MESSAGE); //3. SOAPMessage SOAPMessage msg = MessageFactory.newInstance().createMessage(); SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope(); SOAPBody body = envelope.getBody(); // header SOAPHeader header = envelope.getHeader(); if (header==null) { header = envelope.addHeader(); } QName hname = new QName(ns,"authInfo","nn"); header.addHeaderElement(hname).setValue("USER"); //4. Qname QName ename = new QName(ns,"list","nn");//<nn:add xmlns="xx"> SOAPBodyElement ele = body.addBodyElement(ename); msg.writeTo(System.out); System.out.println("
invoking……"); //5. Dispatch SOAPMessage response = dispatch.invoke(msg); response.writeTo(System.out); // dom Document doc = response.getSOAPBody().extractContentAsDocument(); NodeList nl = doc.getElementsByTagName("user"); JAXBContext ctx = JAXBContext.newInstance(User.class); for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); User u = (User) ctx.createUnmarshaller().unmarshal(n); System.out.println(u.getNickname()); } } catch (SOAPException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } } /** * message login * * : * <fault message="tns:UserException" name="UserException" wsam:Action="http://www.sid.com/webservice/IMyService/login/Fault/UserException"/> * fault output * */ @Test public void test05(){ try { //1. (Service) URL url = new URL(wsdlUrl); QName qname = new QName(ns, "MyServiceImplService"); Service service = Service.create(url, qname); //2. Dispatch //Service.Mode.MESSAGE <> Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns,"MyServiceImplPort"), SOAPMessage.class, Service.Mode.MESSAGE); //3. SOAPMessage SOAPMessage msg = MessageFactory.newInstance().createMessage(); SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope(); SOAPBody body = envelope.getBody(); //4. Qname QName ename = new QName(ns,"login","nn");//<nn:add xmlns="xx"> SOAPBodyElement ele = body.addBodyElement(ename); ele.addChildElement("username").setValue("ss"); ele.addChildElement("password").setValue("ss"); msg.writeTo(System.out); System.out.println("
invoking……"); //5. Dispatch SOAPMessage response = dispatch.invoke(msg); response.writeTo(System.out); } catch (SOAPException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SOAPFaultException e) { System.out.println(e.getMessage());; } } }

서버 쪽 handler 추가:
package org.sid.handler;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

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

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class LicenseHandler implements SOAPHandler<SOAPMessageContext> {
	String ns = "http://www.sid.com/webservice";

	@Override
	public void close(MessageContext context) {
	}

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

	@Override
	public boolean handleMessage(SOAPMessageContext context) {
		System.out.println("=========Server handleMessage=========");
		try {
			//       
			Boolean out = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
			if (!out) {
				SOAPMessage message = context.getMessage();
				//1.  message     header
				SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
				SOAPHeader header = envelope.getHeader();
				SOAPBody body = envelope.getBody();
				Node bn = body.getChildNodes().item(0);
				//  part name
				String partName = bn.getLocalName();
				if ("list".equals(partName)||"addUser".equals(partName)) {
					System.out.println("list or addUser");
					Iterator<SOAPHeaderElement> iterator = header.examineAllHeaderElements();
					if (header==null||!iterator.hasNext()) {
						//        
						SOAPFault fault = body.addFault();
						fault.setFaultString("        ");
						throw new SOAPFaultException(fault);
					}else{
						while (iterator.hasNext()) {
							SOAPHeaderElement ele = iterator.next();
							System.out.println(ele.getTextContent());
						}
					}
					message.writeTo(System.out);
				}
			}
		} catch (SOAPException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}

	@Override
	public Set<QName> getHeaders() {
		return null;
	}

}

handler_chain. xml 파일 내용 은 다음 과 같 습 니 다.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<javaee:handler-chains 
	xmlns:javaee="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<javaee:handler-chain>
		<javaee:handler>
			<javaee:handler-class>org.sid.handler.LicenseHandler</javaee:handler-class>
		</javaee:handler>
	</javaee:handler-chain>
</javaee:handler-chains>

클 라 이언 트 코드 인 스 턴 스:
먼저 wsimport 후 client 테스트 에 가입
package com.sid.test;


import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Holder;
import javax.xml.ws.soap.SOAPFaultException;

import com.sid.webservice.IMyService;
import com.sid.webservice.MyServiceImplService;
import com.sid.webservice.User;

public class SoapClient {
	public static void main(String[] args) throws Exception {
		URL url = new URL("http://localhost:9898/ms?wsdl");
		QName qname = new QName("http://www.sid.com/webservice", "MyServiceImplService");
		MyServiceImplService mis = new MyServiceImplService(url,qname);
		IMyService ms = mis.getMyServiceImplPort();
		try {
//			ms.login("admin","admin");
			User u = new User();
			u.setId(1); u.setUsername("123"); u.setPassword("23"); u.setNickname("23");
			Holder<User> user = new Holder<User>(u);
			ms.addUser(user);
		} catch (SOAPFaultException e) {
			System.out.println(e.getMessage());
		}
	}
}

클 라 이언 트 에 handler 를 추가 하여 header 를 처리 합 니 다:
package org.sid.handler;

import java.io.IOException;
import java.util.Set;

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

import org.w3c.dom.Node;

public class HeaderHandler implements SOAPHandler<SOAPMessageContext> {
	String ns = "http://www.sid.com/webservice";

	@Override
	public void close(MessageContext arg0) {
	}

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

	/**
	 * 
	 * LogicalHandler    SOAPBody  
	 * SOAPHandler    SOAPMessage  
	 *       LogicalHandler
	 *        SOAPHandler
	 * 1.       SOAPHandler(   <SOAPMessageContext>)
	 * 2. handlerMessage     
	 * 3.  Handler
	 * 4.      handler(    MyServiceImplService   @HandlerChain(file="handler_chain.xml"))
	 * 
	 *      
	 * 1.  handler
	 * 2.             (      )
	 */
	@Override
	public boolean handleMessage(SOAPMessageContext context) {
		System.out.println("=========handleMessage=========");
		try {
			//       
			Boolean out = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
			if (out) {
				SOAPMessage message = context.getMessage();
				//1.  message     header
				SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
				//2.  body,   partName
				SOAPBody body = envelope.getBody();
				Node node = body.getChildNodes().item(0);
				String partname = node.getLocalName();
				if ("list".equals(partname)||"addUser".equals(partname)) {
					System.out.println("list or addUser");
					SOAPHeader header = envelope.getHeader();
					if (header==null) {
						header = envelope.addHeader();
					}
					QName qname = new QName(ns, "licenseInfo","nn");
					header.addHeaderElement(qname).setValue("123123");
					message.writeTo(System.out);
					System.out.println();
				}
			}
		} catch (SOAPException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}

	@Override
	public Set<QName> getHeaders() {
		return null;
	}

}

이름 은 wsimport 이후 서버 구현 (MyServiceImplService) 클래스 이름 에 추 가 됩 니 다.
@HandlerChain(file="handler_chain.xml")
handler_chain. xml 파일 내용 은 다음 과 같 습 니 다.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<javaee:handler-chains 
	xmlns:javaee="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<javaee:handler-chain>
		<javaee:handler>
			<javaee:handler-class>org.sid.handler.HeaderHandler</javaee:handler-class>
		</javaee:handler>
	</javaee:handler-chain>
</javaee:handler-chains>

좋은 웹페이지 즐겨찾기