开发ejb模型的web服务
Web属于行业规范,服务可以跨平台和语言
Ejb属于java平台规范
Javaee为web service 提供了两种不同编程模型:
Ejb容器模型
Web容器模型
示例代码如下:
/**
* Copyright (C) 2016
*
* FileName:OrderServiceBean.java
*
* Author:<ahref="mailto:zhenhuayue@sina.com">Retacn</a>
*
* CreateTime: 2016-7-24
*/
// Package Information
package cn.yue.ejb.wsservice.impl;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.jws.WebService;
import cn.yue.ejb.entity.Order;
importcn.yue.ejb.wsservice.OrderService;
/**
* 订单的业务操作
*
* @version
* @Description:
* @author <ahref="mailto:zhenhuayue@sina.com">Retacn</a>
* @since 2016-7-24
*/
// 指定所有方法
@WebService(
// 命名空间
targetNamespace = "http://ws.retacn.cn",
// 端口类型
name = "OrderService",
// 服务名称
serviceName = "OrderServiceBean")
@Stateless
@Remote(OrderService.class)
public class OrderServiceBeanimplements OrderService {
// 可以使用 @WebMethod来单独指定方法
// @WebMethod
@Override
public String getUserName(String name) {
return "retacn";
}
@Override
public Order getOrder(String orderId) {
return new Order(orderId, "测试订单");
}
@Override
public List<Order> getOrders() {
List<Order> orders = new ArrayList<Order>();
orders.add(new Order("001", "测试订单一"));
orders.add(new Order("002", "测试订单二"));
return orders;
}
}
可以通过以下地址查看wadl
http://localhost:9990/console/App.html#webservice-runtime
Web服务的客户端
导入
利用webservice客户端生成工具生成辅助类
Cmd
Cd e:/insallPackage/javaweb/webService/apache-cxf-3.1.1/bin
wsdl2java -d 生成文件目录
http://localhost:8080/HelloWorldEJB/OrderServiceBean/OrderService?wsdl
使用辅肋类调用webservice,示例代码如下:
/**
* Copyright (C) 2016
*
* FileName:WsClient.java
*
* Author:<a href="mailto:zhenhuayue@sina.com">Retacn</a>
*
* CreateTime: 2016-7-24
*/
// Package Information
package cn.retacn.ws.test;
import java.util.List;
import cn.retacn.ws.Order;
import cn.retacn.ws.OrderService;
import cn.retacn.ws.OrderServiceBean;
public class WsClient {
/**
* @param args
*/
public static void main(String[] args) {
OrderServiceBean orderServiceBean = new OrderServiceBean();
OrderService orderService = orderServiceBean.getOrderServicePort();
Order order = orderService.getOrder("123");
System.out.println("order: " + order.getOrderId() +"---" + order.getOrderName());
String userName = orderService.getUserName("123");
System.out.println("用户名为:" + userName);
List<Order> orders =orderService.getOrders();
for (Order order2 : orders) {
System.out.println("---"+ order2.getOrderId() + ":" + order2.getOrderName());
}
}
}
Ejb实现拦截器的功能
1 导入jboss-interceptors-api_1.1_spec-1.0.0.Final.jar
2 编写自定义拦截器