使用AXIS1.4发布WebService_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 使用AXIS1.4发布WebService

使用AXIS1.4发布WebService

 2013/10/14 18:40:39  ducklsl  程序员俱乐部  我要评论(0)
  • 摘要:选择使用AXIS发布WebService,到apache官网上进行下载相关AXIS的相关包下载地址:http://axis.apache.org/axis/java/releases.html1)新建一个Web项目,然后在把下载下来的示例应用中的web.xml和lib目录拷贝进项目中,web.xml文件的代码如下:<servlet><servlet-name>AxisServlet</servlet-name><servlet-class>org
  • 标签:Web Service 使用 发布 Webservice
选择使用AXIS发布WebService,到apache官网上进行下载相关AXIS的相关包
下载地址:http://axis.apache.org/axis/java/releases.html
1)新建一个Web项目,然后在把下载下来的示例应用中的web.xml和lib目录拷贝进项目中,web.xml文件的代码如下:
class="xml"><servlet>
		<servlet-name>AxisServlet</servlet-name>
	<servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>AxisServlet</servlet-name>
		<url-pattern>/servlet/AxisServlet</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>AxisServlet</servlet-name>
		<url-pattern>*.jws</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>AxisServlet</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>
2)新建一个类,查询出相应的库存信息
GoodsinfoSearch类:
public class GoodsinfoSearch {
	private StockDao sd = new StockDao();

	public String queryBacodegoodsinfo(String userName, String sort,
			String page, String pageSize) {
		String message = sd.lastStringData(userName, sort, page, pageSize);
		return message;
	}
}
StockDao类:
public class StockDao {
	private static final String URL = "jdbc:mysql://localhost:3306/stock?useUnicode=true&amp;characterEncoding=gb2312";
	private static final String DRIVER = "com.mysql.jdbc.Driver";
	private static final String USERNAME = "root";
	private static final String PASSWORD = "";
	private int pagesize = 20;
	private int currentpage = 1;
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	private ResultSet res = null;
	static {
		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	// 数据信息
	public List<Barcodegoods> getAllBarcodegoods() {
		String sql = "select * from eas_barcodegoods";
		List<Barcodegoods> bgs = new ArrayList<Barcodegoods>();
		try {
			conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
			pstmt = conn.prepareStatement(sql);
			res = pstmt.executeQuery();
			Barcodegoods bg = null;
			while (res.next()) {
				bg = new Barcodegoods();
				bg.setBarcode(res.getString("barcode"));
				bg.setColor(res.getString("color"));
				bg.setColorid(res.getString("colorid"));
				bg.setGoods_no(res.getString("goods_no"));
				bg.setSize(res.getString("size"));
				bgs.add(bg);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return bgs;
	}
	// 分页查询
	public List<Barcodegoods> queryBarcodegoodsByPage(String sort,
			String page, String pageSize) {
		Integer current;
		Integer pageCount;
		if (page != null && !"".equals(page)) {
			current = Integer.valueOf(page);
		} else {
			current = currentpage;
		}
		if (pageSize != null && !"".equals(pageSize)) {
			pageCount = Integer.valueOf(pageSize);
		} else {
			pageCount = pagesize;
		}
		StringBuffer buffer = new StringBuffer();
		buffer.append("select * from eas_barcodegoods order by barcode ");
		if ("0".equals(sort)) {
			buffer.append("asc ");
		} else if ("1".equals(sort)) {
			buffer.append("desc ");
		}
		buffer.append("limit ");
		buffer.append((current - 1) * pageCount);
		buffer.append(",");
		buffer.append(pageCount);
		String sql = buffer.toString();
		List<Barcodegoods> bgs = new ArrayList<Barcodegoods>();
		try {
			conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
			pstmt = conn.prepareStatement(sql);
			res = pstmt.executeQuery();
			Barcodegoods bg = null;
			while (res.next()) {
				bg = new Barcodegoods();
				bg.setBarcode(res.getString("barcode"));
				bg.setColor(res.getString("color"));
				bg.setColorid(res.getString("colorid"));
				bg.setGoods_no(res.getString("goods_no"));
				bg.setSize(res.getString("size"));
				bgs.add(bg);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return bgs;
	}
	// 分页查询
	public List<Barcodegoods> queryBarcodegoodsByPage(String page,
			String pageSize) {
		Integer current;//当前页码
		Integer pageCount;//每页条数
		if (page != null && !"".equals(page)) {
			current = Integer.valueOf(page);
		} else {
			current = currentpage;
		}
		if (pageSize != null && !"".equals(pageSize)) {
			pageCount = Integer.valueOf(pageSize);
		} else {
			pageCount = pagesize;
		}
		StringBuffer buffer = new StringBuffer();
		buffer.append("select * from eas_barcodegoods ");
		buffer.append("limit ");
		buffer.append((current - 1) * pageCount);
		buffer.append(",");
		buffer.append(pageCount);
		String sql = buffer.toString();
		List<Barcodegoods> bgs = new ArrayList<Barcodegoods>();
		try {
			conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
			pstmt = conn.prepareStatement(sql);
			res = pstmt.executeQuery();
			Barcodegoods bg = null;
			while (res.next()) {
				bg = new Barcodegoods();
				bg.setBarcode(res.getString("barcode"));
				bg.setColorid(res.getString("colorid"));
				bg.setGoods_no(res.getString("goods_no"));
				bg.setSize(res.getString("size"));
				bgs.add(bg);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return bgs;
	}
	// 检查用户
	public boolean checkUser(String userName) {
		String sql = "select * from e_userinfo where name='" + userName + "'";
		boolean flag = false;
		try {
			conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
			pstmt = conn.prepareStatement(sql);
			res = pstmt.executeQuery();
			if (res.next()) {
				flag = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}
	// 返回数据信息
	public String data2String(List<Barcodegoods> bgs, String pageSize,
			String currentPage, Integer allCount) {
		StringBuffer result = new StringBuffer();
		result.append("<resultdetail>");
		if ((bgs != null && bgs.size() > 0)
				&& (pageSize != null && !"".equals(pageSize))
				&& (currentPage != null && !"".equals(currentPage))) {
			Integer size = Integer.valueOf(pageSize);// 每页条数
			Integer all = ((allCount + size - 1) / size);// 总页码
			result.append("<totalcount>");
			result.append(bgs.size());
			result.append("</totalcount>");
			result.append("<allpagenum>");
			result.append(all.toString());
			result.append("</allpagenum>");
			result.append("<nowpagenum>");
			result.append(currentPage);
			result.append("</nowpagenum>");
			result.append("<bglist>");
			Barcodegoods bg = null;
			for (int i = 0; i < bgs.size(); i++) {
				bg = bgs.get(i);
				result.append("<bg>");
				result.append("<barcode>");
				result.append(bg.getBarcode());
				result.append("</barcode>");
				result.append("<color>");
				result.append(bg.getColor());
				result.append("</color>");
				result.append("<colorid>");
				result.append(bg.getColorid());
				result.append("</colorid>");
				result.append("<goods_no>");
				result.append(bg.getGoods_no());
				result.append("</goods_no>");
				result.append("<size>");
				result.append(bg.getSize());
				result.append("</size>");
				result.append("</bg>");
			}
			result.append("</bglist>");
		}
		result.append("</resultdetail>");
		return result.toString();
	}
	public String setResultMessage(String code, String resultMessage) {
		StringBuffer message = new StringBuffer();
		message.append("<resultcode>");
		message.append(code);
		message.append("</resultcode>");
		message.append("<resultmessage>");
		message.append(resultMessage);
		message.append("</resultmessage>");
		return message.toString();
	}
	// 获得最后的数据
	public String lastStringData(String userName, String sort, String page,String pageSize) {
		List<Barcodegoods> bgs = queryBarcodegoodsByPage(sort, page, pageSize);// 获取数据
		Integer allCount = getAllBarcodegoods().size();
		boolean flag = checkUser(userName);
		String message;
		String data;
		if (flag) {
			message = setResultMessage("0", "验证用户成功,获取信息成功");
			data = data2String(bgs, pageSize, page, allCount);
		} else {
			message = setResultMessage("-1", "验证用户失败,获取信息失败");
			data = data2String(null, null, null, null);
		}
		String result = message + data;
		return result;
	}
}
3)在WEB-INF目录下新建server-config.wsdd文件,配置如下:
<?xml version="1.0" encoding="UTF-8"?>     
<deployment xmlns="http://xml.apache.org/axis/wsdd/"    
    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">     
  <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/>     
  <service name="goodsservice" provider="java:RPC">  
  <parameter name="className" value="changluo.bean.GoodsinfoSearch"></parameter>  
  <parameter name="allowedMethods" value="*"></parameter>
  </service>  
  <transport name="http">     
    <requestFlow>     
        <handler type="URLMapper"/>     
    </requestFlow>     
  </transport>     
</deployment>
启动tomcat进行发布,在浏览器地址中输入:http://localhost:8083/webdemo/services/goodsservice看看是否发布成功,如果出现WebService的列表,就表示发布成功叻!
访问WebService,编写客户端进行测试
public static void main(String[] args) throws Exception {
		WebTest wt = new WebTest();
		wt.test();
	}
	public String test() throws Exception {
		String endpoint = "http://localhost:8083/webdemo/services/goodsservice?wsdl";
		Service service = new Service();
		Call call = (Call) service.createCall();
		URL url = new URL(endpoint);
		call.setTargetEndpointAddress(url);
		call.setOperationName("queryBacodegoodsinfo");
		String result1 = (String) call.invoke(new Object[]{"admin","0","3","5"});
		System.out.println(result1);
		return null;
	}
输出结果:
<resultcode>0</resultcode><resultmessage>验证用户成功,获取信息成功</resultmessage><resultdetail><totalcount>5</totalcount><allpagenum>927</allpagenum><nowpagenum>3</nowpagenum><bglist><bg><barcode>B113280002000</barcode><color>黑色</color><colorid>0020</colorid><goods_no>B113280</goods_no><size>00</size></bg><bg><barcode>B113310002000</barcode><color>黑色</color><colorid>0020</colorid><goods_no>B113310</goods_no><size>00</size></bg><bg><barcode>B113330002000</barcode><color>黑色</color><colorid>0020</colorid><goods_no>B113330</goods_no><size>00</size></bg><bg><barcode>B113330002200</barcode><color>浅灰</color><colorid>0022</colorid><goods_no>B113330</goods_no><size>00</size></bg><bg><barcode>B113330006000</barcode><color>大红</color><colorid>0060</colorid><goods_no>B113330</goods_no><size>00</size></bg></bglist></resultdetail>

此结果可以通过JDOM进行解析,解析伪码如下:
public static void parsexml(String xmldoc){
		// 创建一个新的字符串
		StringReader read = new StringReader(xmlDoc);
		// 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
		InputSource source = new InputSource(read);
		// 创建一个新的SAXBuilder
		SAXBuilder sb = new SAXBuilder();
		try {
			// 通过输入源构造一个Document
			Document doc = sb.build(source);
			// 取的根元素
			Element root = doc.getRootElement();
			// System.out.println(root.getName());//输出根元素的名称(测试)
			String code = root.getChildText("resultcode");
			if (code.equals("0")) {
				Element resultdetail = root.getChild("resultdetail");// 正常返回信息
				List odlist = resultdetail.getChildren("bglist");
				for(int i =0;i<odlist.size();i++){
					//进行订单解析工作
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
由于公司对此业务的要求并不高,所以此WebService仅仅是入门的配置案例,没有设计一些高级特性,如Handler和Chain以及像传递复杂对象和文件等等功能的使用。
发表评论
用户名: 匿名