jboss上的soap web service开发示例

以下示例,由jboss quickstart示例代码得来:

一、创建一个常规的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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏码匠的流水账

聊聊flink的RichParallelSourceFunction

flink-streaming-java_2.11-1.6.2-sources.jar!/org/apache/flink/streaming/api/func...

17820
来自专栏码匠的流水账

聊聊storm worker的executor与task

storm-2.0.0/storm-client/src/jvm/org/apache/storm/daemon/worker/Worker.java

11120
来自专栏老马说编程

(33) Joda-Time / 计算机程序的思维逻辑

Joda-Time 上节介绍了JDK API中的日期和时间类,我们提到了JDK API的一些不足,并提到,实践中有一个广泛使用的日期和时间类库,Joda-Tim...

20780
来自专栏小樱的经验随笔

Codeforces 725B Food on the Plane

B. Food on the Plane time limit per test:2 seconds memory limit per test:256 meg...

37280
来自专栏大内老A

谈谈WCF中的Data Contract(2):WCF Data Contract对Generic的支持

通过第一部分的介绍,我们可以体会到,WCF 的Data Contract在CLR Type和Neutral Contract之间搭建了一座桥梁,弥合了.NET世...

20460
来自专栏Clive的技术分享

代码重构的方法

20840
来自专栏函数式编程语言及工具

Akka(23): Stream:自定义流构件功能-Custom defined stream processing stages

    从总体上看:akka-stream是由数据源头Source,流通节点Flow和数据流终点Sink三个框架性的流构件(stream components)...

45480
来自专栏后端沉思录

自定义钉钉机器人报警

按照钉钉的文档来开发,创建机器人后,即可获取Webhook地址,整个过程还是很简单的,以上只是提供了一个思路.

43320
来自专栏码匠的流水账

聊聊storm的LoggingMetricsConsumer

storm-2.0.0/storm-client/src/jvm/org/apache/storm/metric/LoggingMetricsConsumer....

11330
来自专栏Netkiller

Struts Ajax Json

Netkiller Java 手札 Java, Servlet, JavaBean ... 5.4. Ajax + JSON struts.xml 中加入 ...

27230

扫码关注云+社区

领取腾讯云代金券