基于JDK6的JAX-WX为客户端提供XML与JSON格式数据服务,以及客户端采用AXIS调用案例
1:WebService服务端工程目录如下:
需要第三方jar包:gson-2.2.4.jar\javax.xml.bind.jar\commons-lang-2.5.jar
源码如下:
package com.mw.dao; import com.mw.vo.Nsr; import java.util.List; /** * @author y * @date 2015-4-4 9:23:53 * @version 1.0 * @desc */ public interface NsrDao { List<Nsr> getNsrList(); }
package com.mw.dao.impl; import com.mw.dao.NsrDao; import com.mw.vo.Nsr; import java.util.ArrayList; import java.util.List; /** * @author y * @date 2015-4-4 9:27:34 * @version V1.0 * @desc */ public class NsrDaoImpl implements NsrDao{ public List<Nsr> getNsrList() { List<Nsr> list = new ArrayList<Nsr>(); Nsr nsr = new Nsr(); nsr.setNsrsbh("123"); nsr.setNsrmc("name1"); list.add(nsr); nsr = new Nsr(); nsr.setNsrsbh("456"); nsr.setNsrmc("name2"); list.add(nsr); return list; } }
package com.mw.services; import com.google.gson.Gson; import com.mw.dao.NsrDao; import com.mw.dao.impl.NsrDaoImpl; import com.mw.utils.JaxbUtil; import com.mw.vo.Nsr; import com.mw.vo.xml.NsrRoot; import javax.jws.WebMethod; import javax.jws.WebService; /** * @author y * @date 2015-4-4 9:31:11 * @version V1.0 * @desc */ @WebService public class NsrManager { private final NsrDao nd; private final Gson gson; public NsrManager() { nd = new NsrDaoImpl(); gson = new Gson(); } @WebMethod public String getNsrListJSON() { return gson.toJson(nd.getNsrList()); } @WebMethod public String getNsrListXML() { NsrRoot obj = new NsrRoot(); obj.setList(nd.getNsrList()); JaxbUtil jaxbUtil = new JaxbUtil(NsrRoot.class, JaxbUtil.CollectionWrapper.class); return jaxbUtil.toXml(obj, "utf-8"); } @WebMethod public String getNsrObjectXML() { Nsr nsr = nd.getNsrList().get(0); JaxbUtil jaxbUtil = new JaxbUtil(Nsr.class); return jaxbUtil.toXml(nsr, "utf-8"); } }
package com.mw.utils; import java.io.StringWriter; import java.util.Collection; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.annotation.XmlAnyElement; import javax.xml.namespace.QName; import org.apache.commons.lang.StringUtils; /** * @author y * @date 2015-4-4 10:43:08 * @version V1.0 * @desc JAXB工具类用于将Java 对象转换为XML字符串 */ public class JaxbUtil { private JAXBContext jaxbCtx; public JaxbUtil(Class<?>... types) { try { jaxbCtx = JAXBContext.newInstance(types); } catch (JAXBException e) { throw new RuntimeException(e); } } /** * Java Object->Xml * @param root * @param encoding * @return */ public String toXml(Object root, String encoding) { try { StringWriter writer = new StringWriter(); createMarshaller(encoding).marshal(root, writer); return writer.toString(); } catch (JAXBException e) { throw new RuntimeException(e); } } /** * Java Object->Xml, 用于支持对Root Element是Collection的情形 * @param root * @param rootName * @param encoding * @return */ public String toXml(Collection root, String rootName, String encoding) { try { CollectionWrapper wrapper = new CollectionWrapper(); wrapper.collection = root; JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>( new QName(rootName), CollectionWrapper.class, wrapper); StringWriter writer = new StringWriter(); createMarshaller(encoding).marshal(wrapperElement, writer); return writer.toString(); } catch (JAXBException e) { throw new RuntimeException(e); } } /** * 创建Marshaller, 设定encoding * @param encoding * @return */ public Marshaller createMarshaller(String encoding) { try { Marshaller marshaller = jaxbCtx.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); if (StringUtils.isNotBlank(encoding)) { marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); } return marshaller; } catch (JAXBException e) { throw new RuntimeException(e); } } /** * 封装Root Element 是 Collection的情况. */ public static class CollectionWrapper { @SuppressWarnings("unchecked") @XmlAnyElement protected Collection collection; } }
package com.mw.vo; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** * @author y * @date 2015-4-4 9:24:52 * @version V1.0 * @desc */ @XmlRootElement(name="nsr") public class Nsr { private String nsrsbh; private String nsrmc; @XmlElement(name="nsrsbh") public String getNsrsbh() { return nsrsbh; } public void setNsrsbh(String nsrsbh) { this.nsrsbh = nsrsbh; } @XmlElement(name="nsrmc") public String getNsrmc() { return nsrmc; } public void setNsrmc(String nsrmc) { this.nsrmc = nsrmc; } }
package com.mw.vo.xml; import com.mw.vo.Nsr; import java.util.List; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** * @author y * @date 2015-4-4 10:56:11 * @version V1.0 * @desc */ @XmlRootElement(name="nsrs") public class NsrRoot { private List<Nsr> list; @XmlElement(name="nsr") public List<Nsr> getList() { return list; } public void setList(List<Nsr> list) { this.list = list; } }
项目部署后在浏览器中输入:http://localhost:7001/jax_ws_server/NsrManagerService?wsdl
可以看到XML描述。
2:客户端
package com.mw.servlet; import com.mw.utils.AxisInvokeJAXWSUtil; import java.io.IOException; import java.io.PrintWriter; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author y * @date 2015-4-4 10:15:40 * @version 1.0 * @desc */ public class NsrServlet extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/xml;charset=UTF-8"); PrintWriter out = response.getWriter(); String result = ""; try { result = AxisInvokeJAXWSUtil.invoke( "http://services.mw.com/", "getNsrListXML", "http://localhost:7001/jax_ws_server/NsrManagerService?wsdl", null); } catch (Exception ex) { Logger.getLogger(NsrServlet.class.getName()).log(Level.SEVERE, null, ex); } finally{ out.print(result); out.close(); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } }
package com.mw.utils; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.encoding.XMLType; import org.apache.axis.client.Call; import org.apache.axis.client.Service; /** * @author yan * @date 2015-4-2 22:49:33 * @version V1.0 * @desc */ public final class AxisInvokeJAXWSUtil { /** * * @param namespace * @param method * @param wsdl * @param params * @return * @throws Exception */ public static String invoke(String namespace, String method, String wsdl, Object[] params) throws Exception{ Object obj = null; Service service = new Service(); Call call = (Call) service.createCall(); call.setOperationName(new QName(namespace, method)); call.setTargetEndpointAddress(wsdl); if (null != params) { int len = params.length; for (int i = 0; i < len; i++) { call.addParameter("arg" + i, XMLType.XSD_STRING, ParameterMode.IN); } if (len > 0) { call.setReturnType(XMLType.XSD_STRING); } } else { params = new Object[]{}; } obj = call.invoke(params); return obj == null ? "" : obj.toString(); } }
项目存在百度网盘上:http://pan.baidu.com/s/1hqkZh8C