The path of Java learning-spring Httpinvoker Learning

Source: Internet
Author: User
Tags getcolor object serialization

Both Hessian and burlap are HTTP-based, and they all address the problem of the firewall penetration of RMI headaches. But when a serialized object is included in the RPC message passed in, RMI is Hessian and burlap.
Because Hessian and burlap both adopt a private serialization mechanism, RMI uses the serialization mechanism of Java itself. If the data model is very complex, then the HESSIAN/BURLAP serialization model may not be sufficient.
The Spring development team realized the gap before the RMI service and HTTP-based services, and Spring's httpinvoker was born.
Spring's Httpinvoker, which provides RPC on top of HTTP, while also using the Java object Serialization mechanism.

Specific implementation of the program
First, we create an entity class, and implement the Serializable interface

 Packageentity;Importjava.io.Serializable; Public classFruitImplementsSerializable {PrivateString name; PrivateString color;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString GetColor () {returncolor; }     Public voidsetcolor (String color) { This. color =color; }}

Second, create an interface

 Package Service; Import java.util.List; Import entity. Fruit;  Public Interface Fruitservice {    List<Fruit> getfruitlist ();}

Third, create a class, and implement the interface in step two

 PackageService.impl;Importjava.util.ArrayList;Importjava.util.List;Importservice. Fruitservice;Importentity. Fruit; Public classFruitserviceimplImplementsFruitservice { PublicList<fruit>getfruitlist () {List<Fruit> list =NewArraylist<fruit>(); Fruit F1=NewFruit (); F1.setname (Orange); F1.setcolor (Yellow); Fruit F2=NewFruit (); F2.setname (Apple); F2.setcolor (Red);        List.add (F1);        List.add (F2); returnlist; }}

Iv. Configuring SPRINGMVC required information in Web. XML under Web-inf

<Context-param>    <Param-name>Contextconfiglocation</Param-name>    <Param-value>Classpath:applicationContext.xml</Param-value></Context-param><Listener>    <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class></Listener><servlet>    <Servlet-name>Springmvc</Servlet-name>    <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>    <Load-on-startup>1</Load-on-startup></servlet><servlet-mapping>    <Servlet-name>Springmvc</Servlet-name>    <Url-pattern>/</Url-pattern></servlet-mapping> 

V. Configuring bean information to export the service in Applicationcontext.xml

<BeanID= "Furitservice"class= "Service.impl.FruitServiceImpl"></Bean><BeanID= "Furitservice"class= "Org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"P:serviceinterface= "service. Fruitservice "P:service-ref= "Furitservice" /> 

VI. Create the Springmvc-servlet.xml file under Web-inf and configure the UrlMapping

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">    <BeanID= "UrlMapping"class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">        < Propertyname= "Mappings">            <Props>                <propKey= "/fruitservice">Furitservice</prop>            </Props>        </ Property>    </Bean></Beans> 

Vii. Bean information required to obtain service in Applicationcontext.xml writing client

<id= "Getfruitservice"    class= " Org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean "    p:serviceinterface = "service. Fruitservice "    p:serviceurl=" Http://localhost:8080/SpringHttpInvoker/fruitService "  />

Eight, write test code

 Packagetest;Importjava.util.List;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;Importentity. Fruit;Importservice. Fruitservice; Public classTest { Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Fruitservice Fruitservice=(Fruitservice) ctx. Getbean ("Getfruitservice"); List<Fruit> fruitlist =fruitservice.getfruitlist ();  for(Fruit fruit:fruitlist) {System.out.println (Fruit.getcolor ()+ "of" +fruit.getname ()); }    }}

Deploy the project to Tomcat, start the Tomcat service, and run the test code
=========== Console ========

Yellow oranges.
Red apples.

=======================

The path of Java learning-spring Httpinvoker Learning

Related Article

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.