Spring enterprise-level development applications-applications integrated with remote services of axis and spring

Source: Internet
Author: User

The Spring framework provides a good integration mechanism for remote access technologies. Currently, spring supports the following technologies:
1. Remote Method Invocation (RMI ).

Through rmiproxyfactorybean and rmiserviceexporter, spring supports traditional RMI interfaces defined by Java. RMI. Remote and Java. RMI. RemoteException, providing transparent access through RMI;
2. Spring's built-in HTTP invoker Remote Call mechanism.

The spring Development Team is aware of gaps between RMI services and HTTP-based services (such as Hessian and burlap. On the one hand, RMI uses Java Standard Object serialization, but it is difficult to traverse the firewall; on the other hand, Hessian/burlap can work well through the firewall, but uses its own private Object serialization mechanism. Therefore, spring HTTP invoker came into being. HTTP invoker is a brand new remote call model. As part of the Spring framework, HTTP-based remote calls can be performed (through the firewall ), java serialization mechanism is used to reduce the difficulty of development ). HTTP invoker is advantageous when parameters or return values are complex types and cannot be serialized through Hessian serialization. The corresponding support classes in spring are httpinvokerproxyfactorybean and httpinvokerserviceexporter;
3. Hessian.

As a lightweight distributed component technology, Hessian uses its own binary protocol, which seems to be contrary to the trend of a large number of soap applications in the industry, but for some simple remote access services, hessian is sufficient. In spring, through hessianproxyfactorybean and hessianserviceexporter, We can transparently publish the services to be provided externally. Of course, the binary protocol from the company Caucho from California is still used here;
4. burlap.

Burlap is the XML version of Hessian. Spring supports burlapproxyfactorybean and burlapserviceexporter;
5. Jax RPC (Java APIs for XML-based Remote Procedure Call ).

Spring supports Web services accessed through Ajax RPC. In fact, spring's Web Service encapsulation is quite simple. Spring provides a proxy factory Bean: jaxrpcportproxyfactorybean, which allows you to seamlessly integrate a web sevice into your application as the collaborators of another bean, the jaxrpcportproxyfactorybean class uses JAX-RPC to access remote web services.
6. JMS.

Spring supports jmsinvokerserviceexporter and jmsinvokerproxyfactorybean.

Here, we will focus on Spring's support for Web Services. Such support includes two aspects:
First, publish Web Service Based on JAX-RPC.
Second, access the Web service.

For business logic beans that need to be published as Web Services, you only need to inherit the servletendpointsupport class of spring. Suppose there are the following service interfaces and corresponding implementation classes:
Public interface sayhello
{
Public String sayhello (string who );
}
Public class sayhelloimpl implements sayhello
{
Public String sayhello (string who)
{
Return "hello," + WHO;
}
}
To publish sayhelloimpl as a web service, you need to write a web service class to implement the sayhello interface and inherit the servletendpointsupport class. Example:

Public class sayhelloendpoint extends servletendpointsupport implements sayhello
{

Private sayhello sh;

Protected void oninit ()
{
This. SH = (sayhello) getwebapplicationcontext (). getbean ("sayhello ");
}

// Publish the Business Method of the business logic bean as WebService
Public String sayhello (string who) throws RemoteException
{
Return Sh. sayhello (WHO );
}
}

Then, define the spring configuration file and deploy the service bean in the configuration file, for example:

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en"
Http://www.springframework.org/dtd/spring-beans.dtd>

<Beans>
<Bean id = "sayhello" class = "com. ws. sayhelloimpl">
<Property name =>
</Bean>
</Beans>

Modify the Web. xml file to allow the axisservlet to intercept some requests, for example:
<? XML version = "1.0" encoding = "ISO-8859-1"?>
<! Doctype web-app public "-// Sun Microsystems, Inc. // DTD webapplication 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>

<Web-app>
<Listener>
<Listener-class> org. springframework. Web. Context. contextloaderlistener </listener-class>
</Listener>

<! -- Define axisservlet -->
<Servlet>
<Servlet-Name> axisservlet </servlet-Name>
<Servlet-class> org. Apache. axis. Transport. http. axisservlet </servlet-class>
</Servlet>

<! -- Map axisservlet -->
<Servlet-mapping>
<Servlet-Name> axisservlet </servlet-Name>
<URL-pattern>/axis/* </url-pattern>
</Servlet-mapping>
</Web-app>

In the above configuration file, we can see that all requests that match the/axis/* mode are processed by axisservlet. That is, all Web Services released by spring are placed under the Axis Path.
Then write the WSDL file or use a tool to generate it. The WSDL file is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<! -- The root element of WSDL, including schema and other information -->
<Deployment xmlns = "http://xml.apache.org/axis/wsdd"
Xmlns: Java = "http://xml.apache.org/axis/wsdd/providers/java">
<! -- Global configuration of WSDL -->
<Globalconfiguration>
<Parameter name = "adminpassword" value = "admin"/>
<Parameter name = "sendxsitypes" value = "true"/>
<Parameter name = "sendmultirefs" value = "true"/>
<Parameter name = "sendxmldeclaration" value = "true"/>
<Parameter name = "axis. sendminimizedelements" value = "true"/>
<Requestflow>
<Handler type = "Java: org. Apache. axis. Handlers. mongoshandler">
<Parameter name = "Scope" value = "session"/>
</Handler>
<Handler type = "Java: org. Apache. axis. Handlers. mongoshandler">
<Parameter name = "Scope" value = "request"/>
<Parameter name = "extension" value = ". JWR"/>
</Handler>
</Requestflow>
</Globalconfiguration>
<Handler name = "authenticate" type = "Java: org. Apache. axis. Handlers. simpleauthenticationhandler"/>
<Handler name = "localresponder" type = "Java: org. Apache. axis. Transport. Local. localresponder"/>
<Handler name = "urlmapper" type = "Java: org. Apache. axis. Handlers. http. urlmapper"/>
<! -- Define the console of WebService -->
<Service name = "adminservice" provider = "Java: MSG">
<Parameter name = "allowedmethods" value = "adminservice"/>
<Parameter name = "enableremoteadmin" value = "false"/>
<Parameter name = "classname" value = "org. Apache. axis. utils. admin"/>
<Namespace> http://xml.apache.org/axis/wsdd/ </namespace>
</Service>
<! -- Define your own WebService -->
<Service name = "sayhelloservice" provider = "Java: RPC">
<Parameter name = "allowedmethods" value = "*"/>
<! -- Define the implementation class of WebService -->
<Parameter name = "classname" value = "com. ws. sayhelloendpoint"/>
</Service>
<! -- Define the system service of webserivice. -->
<Service name = "version" provider = "Java: RPC">
<Parameter name = "allowedmethods" value = "getversion"/>
<Parameter name = "classname" value = "org. Apache. axis. Version"/>
</Service>
<Transport name = "HTTP">
<Requestflow>
<Handler type = "urlmapper"/>
<Handler type = "Java: org. Apache. axis. Handlers. http. httpauthhandler"/>
</Requestflow>
</Transport>
<Transport name = "local">
<Responseflow>
<Handler type = "localresponder"/>
</Responseflow>
</Transport>
</Deployment>

After these steps, you can publish the common business logic bean deployed in spring into a web service.

The Web Service released by spring is a standard soap-based Web service. Therefore, you can use standard client access or IOC containers to manage web services. When using IOC containers, jaxrpcportproxyfactorybean must be used.
It is very easy to access the Web service using a standard client. You only need to obtain the URL and Method Name of the WebService. According to the above release, we know that the URL of Web service is as follows: http: // localhost: 8080/axis-spring/axis/sayhelloservice. The client code example is as follows:

Public class webserviceclient
{
Public static void main (string ARGs [])
{
System. Out. println ("starting to call WebService ");
Try
{
// The URL of the WebService
String endpoint = "http: // localhost: 8080/axis-spring/axis/sayhelloservice ";
// Create a service object. The service pair is used to create a call object.
Service = new service ();
// Create a call object. The call object is used to call the service.
Call call = (CALL) service. createcall ();
// Set the WebService URL for the call object
Call. settargetendpointaddress (New java.net. URL (endpoint ));
// Set the method name for the call object
Call. setoperationname ("sayhello ");
// Call the WebService method and obtain the returned value
String S = (string) Call. Invoke (new object [] {"Andy Lau "});

System. Out. println (s );
}
Catch (exception E)
{
System. Out. println (E. tostring ());
}
System. Out. println ("the Web Service is successfully called ");
}
}

If you use IOC containers to manage WebService, you must use the jaxrpcportproxyfactorybean class, which is a factory bean. Like all factory beans, requests to this bean will return its products.
To configure jaxrpcportproxyfactorybean, you only need to provide required information such as the URL and namespace of the WebService, and then return the web service. In the spring configuration file, jaxrpcportproxyfactorybean is defined as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en"
Http://www.springframework.org/dtd/spring-beans.dtd>
<Beans>
<! -- Configure the test Bean -->
<Bean id = "test" class = "com. ws. Test">
<Property name = "sayhello">
<Ref local = "sayhelloservice"/>
</Property>
</Bean>
<! -- Configure WebService Bean -->
<Bean id = "sayhelloservice" class = "org. springframework. remoting. jaxrpc. jaxrpcportproxyfactorybean">
<! -- Configure the interface implemented by WebService -->
<Property name = "serviceinterface">
<Value> com. ws. sayhello </value>
</Property>
<! -- Configure the URL of the WebService WSDL -->
<Property name = "wsdldocumenturl">
<Value> http: // localhost: 8080/axis-spring/axis/sayhelloservice? WSDL </value>
</Property>
<! -- Configure the WebService namespace URI -->
<Property name = "namespaceuri">
<Value> http: // localhost: 8080/axis-spring/axis/sayhelloservice </value>
</Property>
<! -- Configure the service name of WebService -->
<Property name = "servicename">
<Value> sayhelloservice </value>
</Property>
<! -- Configure the portname of WebService -->
<Property name = "portname">
<Value> sayhelloservice "</value>
</Property>
</Bean>
</Beans>

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.