首页 > 其他分享 >Spring Jax-Ws. Build and consume Web Services – part 1

Spring Jax-Ws. Build and consume Web Services – part 1

时间:2023-09-27 17:33:36浏览次数:34  
标签:Web http consume Jax public org order soap name


Spring Jax-Ws. Build and consume Web Services – part 1


Posted on  July 12, 2012 Marco


Following the official guide, at the chapter 19 we’ll find the Spring support about web service (honestly, could it miss?).

In this article I’ll describe the use of it with Jax-WS standard (introduced in Java EE 5 and Java 6) and we’ll see how build and consume the web service.

 

First of all, you have to notice that there are a huge variety of methods about build and consume web service. I think I’ve never used two times the same method for building web services. I don’t know why, probably that is the evidence about the large number of technologies for building web services.

My personal opinion is to use technologies which implements java standard, like Jax-WS.

In this first part we’ll take a look about build web service. I’m an old style developer and I’m not able to start from the WSDL definition. So, my approach is to define the interface with methods exposed rather than define the WSDL definition.


package           it.springjaxws;          


           public           interface           OrderService {          


                      


                      public           void           Check(Order order)            throws           Exception;          


                      


                      public           Order Process(Order order);          


                      


                      public           Order Shipping(Order order);          


                      


           }



And its implementation:

package           it.springjaxws;          


           import           java.util.Date;          


                      


           public           class           OrderServiceImpl            implements           OrderService{          


                      


                      @Override          


                      public           void           Check(Order order)            throws           Exception{          


                      if           (order.getItemNumber()==           0           )          


                      {          


                      throw           new           Exception(           "Quantity cannot be 0!!"           );          


                      }            


                      }          


                      


                      @Override          


                      public           Order Process(Order order) {          


                      order.setInvoice(order.getItemNumber() *            1.3           );          


                      return           order;          


                      }          


                      


                      @Override          


                      public           Order Shipping(Order order) {          


                      order.setShippingDate(           new           Date());          


                      return           order;          


                      }          


                      


           }



The Order bean:

package           it.springjaxws;          


           import           java.io.Serializable;          


           import           java.util.Date;          


                      


           import           javax.xml.bind.annotation.XmlRootElement;          


                      


           @XmlRootElement           (name=           "Order"           )          


           public           class           Order            implements           Serializable {          


                      


                      /**          


                      *          


                      */          


                      private           static           final           long           serialVersionUID = 1L;          


                      


                      


                      private           String orderId;          


                      


                      private           double           invoice;          


                      


                      private           int           itemNumber;          


                      


                      private           Date shippingDate;          


                      


                      public           Order()          


                      {}          


                      


                      public           Order(String orderId,            double           invoice,            int           itemNumber)          


                      {          


                      this           .orderId = orderId;          


                      this           .invoice = invoice;          


                      this           .itemNumber = itemNumber;              


                      }          


                      


                      public           String getOrderId() {          


                      return           orderId;          


                      }          


                      public           void           setOrderId(String orderId) {          


                      this           .orderId = orderId;          


                      }          


                      public           double           getInvoice() {          


                      return           invoice;          


                      }          


                      public           void           setInvoice(           double           invoice) {          


                      this           .invoice = invoice;          


                      }          


                      


                      public           int           getItemNumber() {          


                      return           itemNumber;          


                      }          


                      public           void           setItemNumber(           int           itemNumber) {          


                      this           .itemNumber = itemNumber;          


                      }          


                      


                      public           Date getShippingDate() {          


                      return           shippingDate;          


                      }          


                      


                      public           void           setShippingDate(Date shippingDate) {          


                      this           .shippingDate = shippingDate;          


                      }          


                      


                      @Override          


                      public           String toString()          


                      {          


                      return           " orderId:"           + orderId +          


                      " itemNumber:"           + itemNumber +          


                      " invoice:"           + invoice +          


                      " shippingDate:"           + shippingDate;          


                      }          


                      


           }



Now we have to expose the class “OrderService” by an endpoint. For this achieve we need to use a class which extends SpringBeanAutowiringSupport spring’s class. Take a look at the code.

package           it.springjaxws;          


                      


           import           javax.jws.WebMethod;          


           import           javax.jws.WebParam;          


           import           javax.jws.WebService;          


                      


           import           org.springframework.beans.factory.annotation.Autowired;          


           import           org.springframework.web.context.support.SpringBeanAutowiringSupport;          


                      


           @WebService           (serviceName=           "OrderService"           )          


           public           class           OrderServiceEndpoint            extends           SpringBeanAutowiringSupport{          


                      


                      @Autowired          


                      private           OrderService orderService;          


                      


                      @WebMethod          


                      public           void           Check(           @WebParam           (name =            "order"           ) Order order)            throws           Exception          


                      {          


                      orderService.Check(order);          


                      }          


                      


                      @WebMethod          


                      public           Order Process(           @WebParam           (name =            "order"           ) Order order)          


                      {          


                      return           orderService.Process(order);          


                      }          


                      


                      @WebMethod          


                      public           Order Shipping(           @WebParam           (name =            "order"           ) Order order)          


                      {          


                      return           orderService.Shipping(order);          


                      }          


           }



Briefly, we’ve mapped the orderService methods with the WebMethod.

The spring configuration file

<?           xml           version           =           "1.0"           encoding           =           "UTF-8"           ?>          


           <           beans           xmlns           =           "http://www.springframework.org/schema/beans"          


                      xmlns:xsi           =           "http://www.w3.org/2001/XMLSchema-instance"           xmlns:context           =           "http://www.springframework.org/schema/context"          


                      xsi:schemaLocation="http://www.springframework.org/schema/beans          


                      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd          


                      http://www.springframework.org/schema/context          


                      http://www.springframework.org/schema/context/spring-context-3.0.xsd">          


                      


                      <!-- Use @Component annotations for bean definitions -->          


                      <           context:component-scan           base-package           =           "it.springjaxws"           />          


                      


                      <           bean           id           =           "orderService"           class           =           "it.springjaxws.OrderServiceImpl"           />          


                      


                      <           bean           class           =           "org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter"           >          


                      <           property           name           =           "baseAddress"           value           =           "http://localhost:8081/"           />          


                      </           bean           >          


                      


                      <           bean           id           =           "orderServiceEndpoint"           class           =           "it.springjaxws.OrderServiceEndpoint"           />          


                      


           </           beans           >



We expose the web service at port 8081 of localhost.

Deploy it to your tomcat and run. You’ll see something like above code when you’ll browse /OrderServiceEndpoint?WSDL


<?           xml           version           =           "1.0"           encoding           =           "UTF-8"           ?>           <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net.          


                      RI's version is JAX-WS RI 2.1.6 in JDK 6. -->           <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net.          


                      RI's version is JAX-WS RI 2.1.6 in JDK 6. -->          


           <           definitions           xmlns:soap           =           "http://schemas.xmlsoap.org/wsdl/soap/"          


                      xmlns:tns           =           "http://springjaxws.it/"           xmlns:xsd           =           "http://www.w3.org/2001/XMLSchema"          


                      xmlns           =           "http://schemas.xmlsoap.org/wsdl/"           targetNamespace           =           "http://springjaxws.it/"          


                      name           =           "OrderService"           >          


                      <           types           >          


                      <           xsd:schema           >          


                      <           xsd:import           namespace           =           "http://springjaxws.it/"          


                      schemaLocation           =           "http://localhost:8081/OrderServiceEndpoint?xsd=1"           ></           xsd:import           >          


                      </           xsd:schema           >          


                      </           types           >          


                      <           message           name           =           "Check"           >          


                      <           part           name           =           "parameters"           element           =           "tns:Check"           ></           part           >          


                      </           message           >          


                      <           message           name           =           "CheckResponse"           >          


                      <           part           name           =           "parameters"           element           =           "tns:CheckResponse"           ></           part           >          


                      </           message           >          


                      <           message           name           =           "Exception"           >          


                      <           part           name           =           "fault"           element           =           "tns:Exception"           ></           part           >          


                      </           message           >          


                      <           message           name           =           "Process"           >          


                      <           part           name           =           "parameters"           element           =           "tns:Process"           ></           part           >          


                      </           message           >          


                      <           message           name           =           "ProcessResponse"           >          


                      <           part           name           =           "parameters"           element           =           "tns:ProcessResponse"           ></           part           >          


                      </           message           >          


                      <           message           name           =           "Shipping"           >          


                      <           part           name           =           "parameters"           element           =           "tns:Shipping"           ></           part           >          


                      </           message           >          


                      <           message           name           =           "ShippingResponse"           >          


                      <           part           name           =           "parameters"           element           =           "tns:ShippingResponse"           ></           part           >          


                      </           message           >          


                      <           portType           name           =           "OrderServiceEndpoint"           >          


                      <           operation           name           =           "Check"           >          


                      <           input           message           =           "tns:Check"           ></           input           >          


                      <           output           message           =           "tns:CheckResponse"           ></           output           >          


                      <           fault           message           =           "tns:Exception"           name           =           "Exception"           ></           fault           >          


                      </           operation           >          


                      <           operation           name           =           "Process"           >          


                      <           input           message           =           "tns:Process"           ></           input           >          


                      <           output           message           =           "tns:ProcessResponse"           ></           output           >          


                      </           operation           >          


                      <           operation           name           =           "Shipping"           >          


                      <           input           message           =           "tns:Shipping"           ></           input           >          


                      <           output           message           =           "tns:ShippingResponse"           ></           output           >          


                      </           operation           >          


                      </           portType           >          


                      <           binding           name           =           "OrderServiceEndpointPortBinding"           type           =           "tns:OrderServiceEndpoint"           >          


                      <           soap:binding           transport           =           "http://schemas.xmlsoap.org/soap/http"          


                      style           =           "document"           ></           soap:binding           >          


                      <           operation           name           =           "Check"           >          


                      <           soap:operation           soapAction           =           ""           ></           soap:operation           >          


                      <           input           >          


                      <           soap:body           use           =           "literal"           ></           soap:body           >          


                      </           input           >          


                      <           output           >          


                      <           soap:body           use           =           "literal"           ></           soap:body           >          


                      </           output           >          


                      <           fault           name           =           "Exception"           >          


                      <           soap:fault           name           =           "Exception"           use           =           "literal"           ></           soap:fault           >          


                      </           fault           >          


                      </           operation           >          


                      <           operation           name           =           "Process"           >          


                      <           soap:operation           soapAction           =           ""           ></           soap:operation           >          


                      <           input           >          


                      <           soap:body           use           =           "literal"           ></           soap:body           >          


                      </           input           >          


                      <           output           >          


                      <           soap:body           use           =           "literal"           ></           soap:body           >          


                      </           output           >          


                      </           operation           >          


                      <           operation           name           =           "Shipping"           >          


                      <           soap:operation           soapAction           =           ""           ></           soap:operation           >          


                      <           input           >          


                      <           soap:body           use           =           "literal"           ></           soap:body           >          


                      </           input           >          


                      <           output           >          


                      <           soap:body           use           =           "literal"           ></           soap:body           >          


                      </           output           >          


                      </           operation           >          


                      </           binding           >          


                      <           service           name           =           "OrderService"           >          


                      <           port           name           =           "OrderServiceEndpointPort"           binding           =           "tns:OrderServiceEndpointPortBinding"           >          


                      <           soap:address           location           =           "http://localhost:8081/OrderServiceEndpoint"           ></           soap:address           >          


                      </           port           >          


                      </           service           >          


           </           definitions           >



In the next part I describe how consume it.

UPDATE

You can find very useful to host the web services in the same web server port (the 8080 for tomcat). For this purpose you have to use JAX-WS commons from Metro project of glassfish. More info are available at this url http://jax-ws-commons.java.net/spring/

To apply this technology at the previous example, we need to change and update some files.

First, in web.xml, we put the servlet definition

<           web-app           id           =           "WebApp_ID"           version           =           "2.4"          


                      xmlns           =           "http://java.sun.com/xml/ns/j2ee"          


                      xmlns:xsi           =           "http://www.w3.org/2001/XMLSchema-instance"          


                      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee          


                      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">          


                      


                      <           display-name           >SpringJaxWsEmbedded</           display-name           >          


                      


                      <           servlet           >          


                      <           servlet-name           >jaxwsembedded</           servlet-name           >          


                      <           servlet-class           >          


                      com.sun.xml.ws.transport.http.servlet.WSSpringServlet          


                      </           servlet-class           >          


                      </           servlet           >          


                      


                      <           servlet-mapping           >          


                      <           servlet-name           >jaxwsembedded</           servlet-name           >          


                      <           url-pattern           >/order</           url-pattern           >          


                      </           servlet-mapping           >          


                      


                      <!-- Register Spring Listener -->          


                      <           listener           >          


                      <           listener-class           >          


                      org.springframework.web.context.ContextLoaderListener          


                      </           listener-class           >          


                      </           listener           >          


                      


           </           web-app           >



After this, we edit the applicationContext.xml file

<?           xml           version           =           "1.0"           encoding           =           "UTF-8"           ?>          


           <           beans           xmlns           =           "http://www.springframework.org/schema/beans"          


                      xmlns:xsi           =           "http://www.w3.org/2001/XMLSchema-instance"           xmlns:ws           =           "http://jax-ws.dev.java.net/spring/core"          


                      xmlns:wss           =           "http://jax-ws.dev.java.net/spring/servlet"          


                      xmlns:context           =           "http://www.springframework.org/schema/context"          


                      xsi:schemaLocation="http://www.springframework.org/schema/beans          


                      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd          


                      http://jax-ws.dev.java.net/spring/core http://jax-ws.java.net/spring/core.xsd          


                      http://jax-ws.dev.java.net/spring/servlet http://jax-ws.java.net/spring/servlet.xsd          


                      http://www.springframework.org/schema/context          


                      http://www.springframework.org/schema/context/spring-context-3.0.xsd">          


                      


           <!-- Use @Component annotations for bean definitions -->          


                      <           context:component-scan           base-package           =           "it.springjaxwsembedded"           />          


                      


                      <           bean           id           =           "orderService"           class           =           "it.springjaxws.OrderServiceImpl"           />          


                      


                      <           wss:binding           url           =           "/order"           >          


                      <           wss:service           >          


                      <           ws:service           bean           =           "#orderWs"           />          


                      </           wss:service           >          


                      </           wss:binding           >          


                      


                      <!-- Web service methods -->          


                      <           bean           id           =           "orderWs"           class           =           "it.springjaxws.OrderServiceEndpoint"           />          


                      


           </           beans           >



Last point is OrderServiceEndpoint definition

public           class           OrderServiceEndpoint {          


           ...          


           }



We’ve removed the SpringBeanAutowiringSupport extension.

标签:Web,http,consume,Jax,public,org,order,soap,name
From: https://blog.51cto.com/u_16131764/7627106

相关文章

  • Sovit2D组态设计 Web Scada烟气脱硫工艺流程
    前言我国是燃煤大国,燃煤排放的SO₂成为影响我国城市空气质量的主要污染物。因此,锅炉烟气脱硫是减排SO₂的重要手段。建设背景在节能减排的大形势下,钢厂、电厂等烟气脱硫是完成二氧化硫减排任务的重点工作之一。烟气脱硫系统具有很高的复杂性,目前很多脱硫系统出现运行故障多、......
  • 2、nginx常用配置----作为web服务端
    目录环境及目的nginx配置文件特点和结构1特性2主配置文件结构常用全局配置1main段2events段web服务相关配置1server_namerootlisten11listen指令常用选项12server_name定义方式2location21alias定义路径别名3index定义主页4error_page定义错误页面5长连接相关指令6限......
  • ASP.NET Core Web (三) 依赖注入
    依赖注入注入方法方法说明AddTransient每次service请求都是获得不同的实例,暂时性模式AddScoped对于同一个请求返回同一个实例,不同的请求返回不同的实例,作用域模式AddSingleton每次都是获得同一个实例,单一实例模式MVC控制器的DI构造函数输入创建接口......
  • ASP.NET Core Web (中间件)
    中间件中间件类似于装配器,请求处理管道由一系列的中间件组件组成,每个组件在HttpContext上执行操作,按顺序调用管道中的下一个中间件或结束,特定的中间件在通道中装配以后可以获取数据并进行一系列的操作。该图表示request到response的相关流程,每个节点的输入输出。通过调用Use{F......
  • WEBRTC回声消除-AECM算法源码解析之参数解析
    一概述 webrtc针对回声问题一共开源了3种回声消除算法,分别为aec,aecm,以及aec3,其中aec是最早期的版本,在后续的更新中aec3的出现代替了aec在webrtc中的地位,而aecm主要是针对计算能力较弱的移动端或是嵌入式设备而开发的,但同时也带来了它自己的劣势;本文主要介绍AECM算法的计......
  • jenkins自动部署web项目到tomcat 和 本地启动不一致 tomact log中文乱码
    最近项目使用jenkins来自动化部署,部署后发现catalina.out日志中文是乱码的。如果使用手动部署不经过Jenkins的话项目就一切正常。所以我想有可能是因为jenkins部署的时候,导致目标服务器那边获取不到相应的环境变量。在tomcat日志中发现,手动启动使用的是自己安装的JDK,而用jenkins......
  • 基于web房屋租售管理系统-计算机毕业设计源码+LW文档
    摘 要当今,在这个信息化的时代,人们的生活越来越便利。因此如果运用java技术建设房屋租售管理系统系统使其与互联网有效的结合起来,实现房屋租售管理系统系统的网络化,为学校师生乃至社会提供更为全面、便捷的服务。根据本系统的研究现状和发展方向,首先系统从需求分析、结构设计、数......
  • 向目标输入框输入值(WebDriverWait判断是否有该输入框)
    #导包fromseleniumimportwebdriverfromselenium.webdriver.common.byimportByfromtimeimportsleepfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasEC#浏览器驱动driver=webdriver.Ch......
  • Selenium进阶——解决web 自动化中上传文件的问题
    在做ui自动化测试中,经常会遇到上传文件或者图片的场景,通常的解决方案是自动化工具+autoIT,在这里我介绍一种通过jdk自带api——java.awt.Robot来解决类似问题的方法。java.awt.Robot类主要用于模拟用户点击键盘上的按键,或者模拟用户敲击鼠标等动作。在做web测试时,弹出窗口如下图所......
  • 12-web前端轮播图案例 (小米商城)
    说明:轮播图在前端开发中是一种常见的元素,通常用于展示一系列的图片或者内容,并通过滑动或者点击的方式进行切换。使用JavaScript来实现轮播图有以下几个意义:提升用户体验:轮播图可以在有限的空间内展示更多的内容,为用户提供更多的信息。同时,轮播图也具有较好的视觉效果,可以吸引用......