一、创建一个常规的dynamic web项目,建议支持maven ,项目的关键属性参考下图:
二、定义服务接口及参数对象
HelloWorldService
1 package org.jboss.as.quickstarts.wshelloworld; 2 3 import java.util.List; 4 5 import javax.jws.WebMethod; 6 import javax.jws.WebService; 7 8 9 10 @WebService(targetNamespace = "http://www.jboss.org/jbossas/quickstarts/wshelloworld/HelloWorld") 11 public interface HelloWorldService { 12 13 14 @WebMethod 15 public String sayHello(); 16 17 18 @WebMethod 19 public String sayHelloToName(String name); 20 21 22 @WebMethod 23 public String sayHelloToNames(List<String> names); 24 }
OrderService
1 package org.jboss.as.quickstarts.wshelloworld; 2 3 import java.util.List; 4 5 import javax.jws.WebMethod; 6 import javax.jws.WebService; 7 8 import org.jboss.as.quickstarts.wshelloworld.model.Order; 9 import org.jboss.as.quickstarts.wshelloworld.model.Product; 10 11 @WebService(targetNamespace = "http://www.jboss.org/jbossas/quickstarts/wshelloworld/Order") 12 public interface OrderService { 13 14 @WebMethod 15 Order orderProduct(List<Product> products); 16 17 }
Product
1 package org.jboss.as.quickstarts.wshelloworld.model; 2 3 import java.io.Serializable; 4 5 public class Product implements Serializable { 6 7 private static final long serialVersionUID = -2823691181671902736L; 8 9 private String productName; 10 11 public String getProductName() { 12 return productName; 13 } 14 15 public void setProductName(String productName) { 16 this.productName = productName; 17 } 18 19 public double getAmount() { 20 return amount; 21 } 22 23 public void setAmount(double amount) { 24 this.amount = amount; 25 } 26 27 private double amount; 28 29 30 31 }
Order
1 package org.jboss.as.quickstarts.wshelloworld.model; 2 3 import java.io.Serializable; 4 import java.util.List; 5 6 public class Order implements Serializable { 7 8 private static final long serialVersionUID = 5377627580623153289L; 9 10 private List<Product> products; 11 12 public List<Product> getProducts() { 13 return products; 14 } 15 16 public void setProducts(List<Product> products) { 17 this.products = products; 18 } 19 20 public double getAmount() { 21 return amount; 22 } 23 24 public void setAmount(double amount) { 25 this.amount = amount; 26 } 27 28 public String getOrderNo() { 29 return orderNo; 30 } 31 32 public void setOrderNo(String orderNo) { 33 this.orderNo = orderNo; 34 } 35 36 private double amount; 37 private String orderNo; 38 39 }
三、服务接口实现
HelloWorldServiceImpl
1 package org.jboss.as.quickstarts.wshelloworld; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import javax.jws.WebService; 7 8 @WebService(serviceName = "HelloWorldService", portName = "HelloWorld", name = "HelloWorld", endpointInterface = "org.jboss.as.quickstarts.wshelloworld.HelloWorldService", targetNamespace = "http://www.jboss.org/jbossas/quickstarts/wshelloworld/HelloWorld") 9 public class HelloWorldServiceImpl implements HelloWorldService { 10 11 @Override 12 public String sayHello() { 13 return "Hello World!"; 14 } 15 16 @Override 17 public String sayHelloToName(final String name) { 18 final List<String> names = new ArrayList<String>(); 19 names.add(name); 20 21 return sayHelloToNames(names); 22 } 23 24 @Override 25 public String sayHelloToNames(final List<String> names) { 26 return "Hello " + createNameListString(names); 27 } 28 29 private String createNameListString(final List<String> names) { 30 31 if (names == null || names.isEmpty()) { 32 return "Anonymous!"; 33 } 34 35 final StringBuilder nameBuilder = new StringBuilder(); 36 for (int i = 0; i < names.size(); i++) { 37 38 if (i != 0 && i != names.size() - 1) 39 nameBuilder.append(", "); 40 else if (i != 0 && i == names.size() - 1) 41 nameBuilder.append(" & "); 42 43 nameBuilder.append(names.get(i)); 44 } 45 46 nameBuilder.append("!"); 47 48 return nameBuilder.toString(); 49 } 50 }
OrderServiceImpl
1 package org.jboss.as.quickstarts.wshelloworld; 2 3 import java.text.SimpleDateFormat; 4 import java.util.ArrayList; 5 import java.util.Calendar; 6 import java.util.Date; 7 import java.util.List; 8 9 import javax.enterprise.inject.New; 10 import javax.jws.WebService; 11 import javax.lang.model.element.VariableElement; 12 13 import org.jboss.as.quickstarts.wshelloworld.model.Order; 14 import org.jboss.as.quickstarts.wshelloworld.model.Product; 15 16 @WebService(serviceName = "OrderService", portName = "Order", name = "Order", endpointInterface = "org.jboss.as.quickstarts.wshelloworld.OrderService", targetNamespace = "http://www.jboss.org/jbossas/quickstarts/wshelloworld/Order") 17 public class OrderServiceImpl implements OrderService { 18 19 @Override 20 public Order orderProduct(List<Product> products) { 21 Order order = new Order(); 22 order.setProducts(products); 23 24 double sum = 0; 25 for (Product p : products) { 26 sum += p.getAmount(); 27 28 } 29 30 String orderNo = new SimpleDateFormat("yyMMddHHmmssSSS") 31 .format(Calendar.getInstance().getTime()); 32 33 order.setAmount(sum); 34 order.setOrderNo(orderNo); 35 return order; 36 } 37 38 }
四、配置web.xml (可选)
1 <?xml version="1.0"?> 2 3 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 6 <servlet> 7 <servlet-name>HelloWorldService</servlet-name> 8 <servlet-class>org.jboss.as.quickstarts.wshelloworld.HelloWorldServiceImpl</servlet-class> 9 </servlet> 10 <servlet-mapping> 11 <servlet-name>HelloWorldService</servlet-name> 12 <url-pattern>/HelloWorld/*</url-pattern> 13 </servlet-mapping> 14 15 <servlet> 16 <servlet-name>OrderService</servlet-name> 17 <servlet-class>org.jboss.as.quickstarts.wshelloworld.OrderServiceImpl</servlet-class> 18 </servlet> 19 <servlet-mapping> 20 <servlet-name>OrderService</servlet-name> 21 <url-pattern>/Order/*</url-pattern> 22 </servlet-mapping> 23 </web-app>
该文件开发时,位置于src/main/webapp/WEB-INF下,配置web.xml的主要作用是给WebService指定显示的别名以及访问时的url路径,这是可选步骤
五、部署到Jboss
部署完成后,可在jboss管理面板中看到这二个web service
六、客户端程序验证(c#版)
可用任何语言调用java web service验证,下面是c#版的示例代码:
1 using System; 2 using java_ws_client.java.hello; 3 using java_ws_client.java.order; 4 5 namespace java_ws_client 6 { 7 class Program 8 { 9 static void Main(string[] args) 10 { 11 Console.WriteLine("hello world service is testing..."); 12 13 using (HelloWorldServiceClient client = new HelloWorldServiceClient()) 14 { 15 var msg = client.sayHelloToName("jimmy"); 16 Console.WriteLine(msg); 17 } 18 19 Console.WriteLine("\norder service is testing..."); 20 21 using (OrderServiceClient client = new OrderServiceClient()) 22 { 23 var order = client.orderProduct(new product[] { new product() { productName = "book", amount = 2 }, new product() { productName = "iphone", amount = 3 } }); 24 25 Console.WriteLine(String.Format("orderNo:{0},amount:{1},product:{2}", order.orderNo, order.amount, order.products.Length)); 26 } 27 28 Console.ReadKey(); 29 } 30 } 31 }
服务端演示源码:helloworld-ws.zip