How EJB publishes Web services

Source: Internet
Author: User


Definition
We often hear that Web services are used in xx projects. So what is Web Service?
First, let's take a look at the Web Service. The Web Service technology enables different applications running on different machines to exchange data or integrate with each other without additional, specialized third-party software or hardware. Applications implemented according to Web Service specifications can exchange data with each other regardless of the language, platform, or internal protocol they use. A Web Service is a self-describing and self-contained available network module that can perform specific business functions.
Web service is a platform-independent, low-coupling, self-contained, programmable web-based application that can use open XML (a subset of standard General Markup Language) standards are used to describe, publish, discover, coordinate, and configure these applications for Distributed interoperability applications.

Preparation
Publishing EJB into a Web service is not difficult in the EJB container, as long as the container implements the Java EE JAX-WS standard on the line, developers to do is to add a little bit of Annotation about WebService, package and release it to the application container.

Procedure
Create a Web Service

  • Create a POJO endpoint
The server no longer needs to enforce that WebService must implement an interface, and a common POJO + EJB annotation class can be used as a WebService server class.
HelloWorld server code
Package com. foshanshop. ws; import javax. jws. webMethod; import javax. jws. webService; import javax. jws. soap. SOAPBinding; @ WebService (name = "HelloWorld", targetNamespace = "http://com.foshanshop.ws", serviceName = "HelloWorldService") @ SOAPBinding (style = SOAPBinding. style. RPC) public class HelloWorldService {@ WebMethodpublic String sayHello (String name) {System. out. println ("the client calls the server code"); return name + "said: this is a simple W Eb testing service! ";}}

@ WebService: This annotation is placed before the Java class. Some methods that declare this class can be published as Web Services. @ WebService attributes are used to set some configuration information when a Web Service is published. common attributes are described as follows. the name of the nameWeb service. The name attribute of the WSDL: portType element in the wsdl is consistent with that of it. The default value is the name of a Java class or interface. 2. The service name of the serviceNameWeb Service. The name attribute of the WSDL: service element is consistent with that of the Service element. The default value is the Java class name + "service ".
3. The namespace used by the targetNamespaceWSDL file. Other XML documents generated in the Web Service also use this as the namespace. @ SOAPBinding () indicates that the service can be mapped to a SOAP message. Style is used to specify the encoding method for SOAP message requests and responses. @ WebMethod: The comment is placed before the method to be published as a Web service.
  • Define an endpoint as a Servlet
Web. xml configuration
<servlet>   <servlet-name>HelloWorldService</servlet-name>   <servlet-class>com.foshanshop.ws.HelloWorldService</servlet-class></servlet><servlet-mapping>   <servlet-name>HelloWorldService</servlet-name>   <url-pattern>/HelloWorldService/*</url-pattern></servlet-mapping>

  • Package an endpoint into a war package
After defining the Servlet, compress the server class into a war package and deploy it to the JBoss container.
Note: Both the POJO endpoint file and the web. xml file are required.
After completing the above steps, we have completed the development of a Web Service. Now we can view the Web Service just released through the Jboss management platform and open the link http: // localhost: 8080/jbossws, see the following interface


Click View a list of deployed services to View the published Web Services, as shown in figure



In, you can click the path http: // 127.0.0.1: 8080/ws_01/HelloWorldService under ServiceEndpointAddress? The wsdl accesses its wsdl description. The wsdl description file is automatically generated by the container when the application is released. The output is as follows:


Create a client
Create a java project named ws_01_client, create a Web Service Client, and enter http: // 127.0.0.1: 8080/ws_01/HelloWorldService In the wsdl url? Wsdl, all the way to the next step.
After the client is created, a test class is written. The Code is as follows:
Package com. foshanshop. ws; import ws.foshanshop.com. helloWorld; import ws.foshanshop.com. helloWorldService; public class TestHelloWorld {/*** @ param args */public static void main (String [] args) {HelloWorldService helloWorldService = new HelloWorldService (); HelloWorld helloWorld = helloWorldService. getHelloWorldPort (); System. out. println (helloWorld. sayHello (" "));}}


Output
  • Client output
Xiao Xing said: this is a simple Web Testing Service!
  • Server output
16:46:26, the 474 INFO [STDOUT] client called the server code

Summary
In fact, it is relatively simple to publish a Web Service using EJB. Here I only list a method to call Web Service using java language. Others are similar.
In fact, the main goal of WebService is cross-platform interoperability. To achieve this goal, WebService is fully based on XML (Extensible Markup Language), XSD (XMLSchema), and other standards independent of the platform and software vendors, is a new platform for creating interoperable and distributed applications. From this we can see that in the following three cases, using WebService will bring great benefits.
  • Cross-firewall communication
In an application with many interactions between the user interface and the middle layer, using the WebService structure can save 20% of the development time spent on User Interface Programming. In addition, such an intermediate layer composed of WebService can be reused in application assembly or other scenarios. Finally, the logic and data of applications are "Exposed" Through WebService, and customers on other platforms can reuse these applications.
  • Application Integration
Enterprise-level application developers know that enterprises often need to integrate various programs written in different languages and run on different platforms, this integration will take a lot of development effort. Through WebService, applications can expose functions and data in a standard way for other applications.
  • B2B Integration
The biggest benefit of using WebService to implement B2B integration is that it can easily implement interoperability. As long as the business logic is exposed and becomes a WebService, any designated partner can call these business logic, regardless of the platform on which their system runs and the development language. This greatly reduces the time and cost spent on B2B integration, so that many small and medium-sized enterprises that could not afford EDI can also implement B2B integration.
  • Software and data Reuse
Software reuse is a big topic. There are many forms of reuse, and the degree of reuse is small. The most basic form is source code module or class-Level Reuse, And the other form is binary form Component reuse.
WebService allows code reuse while reusing the data behind the code. To use WebService, you no longer need to purchase and install software components from a third party, as before, and then call these components from the application. You only need to directly call the remote WebService.

From the above discussion, we can see that WebService is most useful for Web-based Interoperability or remote calls. However, in some cases, WebService cannot bring any benefits at all.
  • Standalone application
Currently, enterprises and individuals still use many desktop applications. Some of them only need to communicate with other programs on the local machine. In this case, it is best not to use WebService, as long as you use a local API. COM is very suitable for working in this case because it is small and fast. The same is true for the software running on the same server. It is best to directly use COM or other local APIs to call applications. Of course, WebService can also be used in these scenarios, but it will not only consume too much, but will not bring any benefits.
  • Homogeneous LAN applications
In many applications, all programs are developed using VB or VC, all use COM on Windows platform, and all run on the same LAN. For example, two server applications need to communicate with each other, or one Win32 or WinForm client program needs to connect to another server on the LAN. In these programs, using DCOM is much more effective than SOAP/HTTP. Similarly, if A. NET program is connected to another. NET program on the LAN, use. NETremoting. Interestingly, in. NETremoting, you can also specify SOAP/HTTP for WebService calling. However, it is better to call RPC directly through TCP, which will be much more effective.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.