Java learning path-Burlap Learning
Today, let's learn about Burlap. Burlap is an XML-based Remote Call technology, but unlike other XML-based remote technologies (such as SOAP or XML-RPC), Burlap's message structure is as simple as possible, no additional external Definition Language (such as WSDL or IDL) is required ). Burlap and Hessian are the same to a large extent. The only difference is that Hessian messages are binary messages, while Burlap messages are XML messages. (The implementation of Burlap and Hessian code is also very similar.) Next, let's take a look at the implementation of the Code: 1. First, we should create an entity class. Here we do not need to implement the Serializable interface to copy the code package entity; public class Food {private String name; private double price; public String getName () {return name;} public void setName (String name) {this. name = name;} public double getPrice () {return price;} public void setPrice (double price) {this. price = price ;}} copy code 2. Let's define an interface to copy the code package service; import Java. util. list; import entity. food; public interface FoodService {List <Food> getFoodList ();} copy code 3 and define a class to implement the interface in step 2, and inherit the BurlapServlet class (here you need to use the jar file Burlap, you can download the http://www.findjar.com/jar/burlap/jars/burlap-2.1.7.jar.html here) copy the code package service. impl; import java. util. arrayList; import java. util. list; import service. foodService; import com. caucho. burlap. server. burlapServlet; import entity. food; public CIA Ss FoodServiceImpl extends BurlapServlet implements FoodService {public List <Food> getFoodList () {List <Food> list = new ArrayList <Food> (); Food f1 = new Food (); f1.setName ("Pickled Fish"); f1.setPrice (25); Food f2 = new Food (); f2.setName ("sweet and sour fish"); f2.setPrice (23); list. add (f1); list. add (f2); return list;} copy code 4, now we can in the WEB-INF web. configure a servlet in xml (the servlet can also be configured in Hessian) and copy the Code <servlet> <servlet-name> food </servle T-name> <servlet-class> service. impl. foodServiceImpl </servlet-class> </servlet> <servlet-mapping> <servlet-name> food </servlet-name> <url-pattern>/food </url-pattern> </servlet-mapping> copy Code 5. Let's write the test code, check the result and copy the code package test; import java. util. list; import service. foodService; import com. caucho. burlap. client. burlapProxyFactory; import entity. food; public class Test {public static void main (String [] args ){ String url = "http: // 127.0.0.1: 8080/test/food"; BurlapProxyFactory factory = new BurlapProxyFactory (); try {FoodService foodSevice = (FoodService) factory. create (FoodService. class, url); List <Food> foodList = foodSevice. getFoodList (); for (Food food: foodList) {System. out. println (food. getName () + ":" + food. getPrice () + "RMB. ") ;}} Catch (Exception e) {e. printStackTrace () ;}} the result displayed in the copy code console is: ========== console ========== pickled fish: 25.0 yuan. Sugar and vinegar fish: 23.0 yuan. ==================================== Next let's take a look at Spring integrated Burlap, which is basically the same as Spring integrated Hessian. Spring integrates Burlap 1. Let's define an interface to copy the code package service; import java. util. list; import entity. food; public interface FoodService {List <Food> getFoodList ();} copy Code 2, define a class, implement the interface in step 2 to copy the code package service. impl; import java. util. arrayList; import java. util. list; import service. foodService; import entity. food; public class FoodServiceImpl implements FoodService {public List <Food> getFoodList () {List <Food> list = new Array List <Food> (); Food f1 = new Food (); f1.setName ("Pickled Fish"); f1.setPrice (25); Food f2 = new Food (); f2.setName ("sweet and sour fish"); f2.setPrice (23); list. add (f1); list. add (f2); return list;} copy Code 3, we can in the WEB-INF web. to configure SpringMVC in xml, You need to copy the Code <context-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: applicationContext. xml </param-value> </context-param> <listener-class> org. springframew Ork. web. context. contextLoaderListener </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 4 in applicationContext. configure the bean information copy code for the Service to be exported in xml <be An id = "foodService" class = "service. impl. foodServiceImpl "> </bean> <bean id =" FoodService "class =" org. springframework. remoting. caucho. burlapServiceExporter "p: serviceInterface =" service. foodService "p: service-ref =" foodService "/> copy Code 5. Create a WEB-INF file under the springMvc-servlet.xml and configure information replication 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 na Me = "mappings"> <props> <prop key = "/foodService"> FoodService </prop> </props> </property> </bean> </beans> code 6: applicationContext in the client program. <bean id = "getFoodService" class = "org. springframework. remoting. caucho. burlapProxyFactoryBean "p: serviceInterface =" service. foodService "p: serviceUrl =" http: // 127.0.0.1: 8080/test/foodService "/> 7. Now let's write 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. food; import service. foodService; public class Test {public static void main (String [] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext. xml "); FoodService foodService = (FoodService) ctx. getBean ("getFoodService"); List <Food> foodLi St = foodService. getFoodList (); for (Food food: foodList) {System. out. println (food. getName () + ":" + food. getPrice () + "RMB. ");}}}