Java learning path-Spring's HttpInvoker Learning

Source: Internet
Author: User
Tags getcolor

Java learning path-Spring's HttpInvoker Learning
Hessian and Burlap are both based on HTTP, and both of them solve the firewall penetration problem caused by RMI. However, when the passed RPC message contains a serialized object, RMI wins Hessian and Burlap. Hessian and Burlap both adopt private serialization mechanisms, while RMI uses Java serialization mechanisms. If the data model is very complex, the Hessian/Burlap serialization model may not be competent. The Spring development team realized the gaps before RMI services and HTTP-based services. Spring's HttpInvoker came into being. Spring's HttpInvoker, which provides RPC based on HTTP and uses the Java object serialization mechanism. Program Implementation 1. First we create an object class and implement the Serializable interface to copy the code package entity; import java. io. serializable; public class Fruit implements Serializable {private String name; private String color; public String getName () {return name;} public void setName (String name) {this. name = name;} public String getColor () {return color;} public void setColor (String color) {this. color = color ;}} copy code 2. Create an interface to copy the code package se Rvice; import java. util. list; import entity. fruit; public interface FruitService {List <Fruit> getFruitList ();} copy code 3. Create a class and copy the code package service using the interface in step 2. impl; import java. util. arrayList; import java. util. list; import service. fruitService; import entity. fruit; public class FruitServiceImpl implements FruitService {public List <Fruit> getFruitList () {List <Fruit> list = new ArrayList <Fruit> (); Fruit f1 = n Ew Fruit (); f1.setName ("orange"); f1.setColor ("yellow"); Fruit f2 = new Fruit (); f2.setName ("apple "); f2.setColor ("red"); list. add (f1); list. add (f2); return list;} copy Code 4. web. the information replication Code <context-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: applicationContext. xml </param-value> </context-param> <listener-class> org. springframework. web. context. conte XtLoaderListener </listener-class> </listener> <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> copy Code 5 in applicationContext. xml configuration: bean information of the service to be exported <bean id = "furitService" clas S = "service. impl. fruitServiceImpl "> </bean> <bean id =" FuritService "class =" org. springframework. remoting. httpinvoker. httpInvokerServiceExporter "p: serviceInterface =" service. fruitService "p: service-ref =" furitService "/> 6. Create a WEB-INF file under the springMvc-servlet.xml and configure the urlMapping copy Code <? 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: 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"> <bean id = "urlMapping" class = "org. springframework. web. servlet. handler. simpleUrlHandlerMapping "> <property name =" mappings "> <props> <prop key ="/fruitService "> FuritService </prop> </props> </property> </bean> </beans> copy code 7 in applicationContext. <bean id = "getFruitService" class = "org. springframework. remoting. httpinvoker. httpInvokerProxyFactoryBean "p: serviceInterface =" service. fruitService "p: serviceUrl =" http: // localhost: 8080/SpringHttpInvoker/fruitService "/> 8. Compile the test code and copy the code package test; import java. util. list; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; import entity. fruit; import service. fruitService; public class Test {public static void main (String [] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext. xml "); FruitService fruitService = (FruitService) ctx. getBean ("getFruitService"); List <Fruit> fruitList = fruitService. getFruitList (); for (Fruit fruit: fruitList) {System. out. println (fruit. getColor () + "+ fruit. getName ());}}

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.