Hessian是一个采用二进制格式传输的服务框架,相对传统soap web service,更轻量,更快速。官网地址:http://hessian.caucho.com/
先上个效果图,在客户端界面通过ID查询后调用后台的Hession服务获取用户数据。
工程分为三个部分,一个WEB工程,一个公共接口工程,一个客户端工程,WEB工程跟客户端工程通过Maven依赖于公共接口工程。
1.通过Maven新建一个名称为HessianInterfaces的工程,Archetype选择maven-archetype-quickstart。
其pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ken.mv</groupId> <artifactId>HessianInterfaces</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>HessianInterfaces</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
在工程内创建一个User的实体类:
package com.ken.entity; public class User implements java.io.Serializable { public User(){} public User(int id,String userName,String sex){ this.id = id; this.userName = userName; this.sex = sex; } private int id; private String userName; private String sex; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
接着在工程内创建一个接口文件:
package com.ken.service; import com.ken.entity.User; public interface IHessianService { public User getUserById(int id); //通过id查询用户 }
至此接口工程就制作完成了。
2.建立WEB工程,其pom.xml内依赖于hessian、spring、接口工程
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ken.mv</groupId> <artifactId>KenMaven</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>KenMaven Maven Webapp</name> <url>http://maven.apache.org</url> <repositories> <repository> <id>maven-net-cn</id> <name>aliyu-Maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <dependencies>
<!--接口工程--> <dependency> <groupId>com.ken.mv</groupId> <artifactId>HessianInterfaces</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.7</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.8.RELEASE</version> </dependency> </dependencies> <build> <finalName>KenMaven</finalName> </build> </project>
创建接口的实现类,并简单的初始化一些数据:
package com.ken.service.impl; import com.ken.service.IHessianService; import java.util.ArrayList; import java.util.List; import com.ken.entity.User; public class HessianServiceImpl implements IHessianService { static List<User> list = new ArrayList<User>(); static { list.add(new User(1, "Ken", "Male")); list.add(new User(2, "Jack", "Male")); list.add(new User(3, "Lucy", "Male")); list.add(new User(4, "Michael", "Male")); list.add(new User(5, "Pearl", "Female")); } public User getUserById(int id) { for (User u : list) { if (u.getId() == id) { return u; } } return null; } }
在resources文件夹内添加hession-context.xml文件
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- BeanNameUrlHandlerMapping的作用是,当<bean>的name属性以/开头的时候,映射为url请求。 --> <!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> --> <bean id="hessianServiceImpl" class="com.ken.service.impl.HessianServiceImpl" /> <!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务 --> <bean name="/ihessian.service" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="hessianServiceImpl" /> <!-- Hessian服务的接口 --> <property name="serviceInterface" value="com.ken.service.IHessianService" /> </bean> </beans>
web.xml如下,用spring拦截.service结尾的hession服务:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Archetype Created Web Application</display-name> <!-- spring mvc--> <servlet> <servlet-name>dispatherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--hessian--> <servlet> <servlet-name>hessianServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:hessian-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hessianServlet</servlet-name> <url-pattern>*.service</url-pattern> </servlet-mapping> </web-app>
此时在Tomcate内发布工web工程。访问hession服务,出现如下错误表示配置成功。
3.跟第一步一样创建一个普通的Maven工程。
pom.xml文件如下,也需要添加接口工程的依赖。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ken.mv</groupId> <artifactId>Client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Client</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <repositories> <repository> <id>maven-net-cn</id> <name>aliyu-Maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.7</version> </dependency> <dependency> <groupId>com.ken.mv</groupId> <artifactId>HessianInterfaces</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.8.RELEASE</version> </dependency> <dependency> <groupId>org.eclipse.swt</groupId> <artifactId>org.eclipse.swt.win32.win32.x86</artifactId> <version>4.3</version> </dependency> </dependencies> </project>
创建hessian-client.xml文件:
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="hessianService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl"> <value>http://localhost:8080/WebApp/ihessian.service</value> </property> <property name="serviceInterface"> <value>com.ken.service.IHessianService</value> </property> </bean> </beans>
窗体(SWT绘制)实现及调用服务如下:
package com.ken.mv.Client; import org.eclipse.swt.SWT; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ken.entity.User; import com.ken.service.IHessianService; public class App { public static void main( String[] args ) { Display display = new Display(); final Shell shell = new Shell(display,SWT.SHELL_TRIM); shell.setSize(300, 300); shell.setText("Hessian"); shell.setLayout(new FormLayout()); final Group group = new Group(shell,SWT.SHADOW_ETCHED_IN); group.setText("用户查询"); final GridData gd_group = new GridData(SWT.FILL,SWT.FILL,true,true); gd_group.heightHint = 100; //group.setData(gd_group); org.eclipse.swt.layout.GridLayout gridLayout = new org.eclipse.swt.layout.GridLayout(2, true); gridLayout.verticalSpacing = 5; gridLayout.horizontalSpacing = 5; gridLayout.marginLeft = 10; gridLayout.marginRight = 10; gridLayout.marginTop = 10; group.setLayout(gridLayout); final Label idLabel = new Label(group,SWT.NONE); final GridData gd_idLabel = new GridData(SWT.LEFT,SWT.CENTER,false,false); idLabel.setText("ID:"); idLabel.setData(gd_idLabel); final Text idText = new Text(group,SWT.BORDER); final GridData gd_idText = new GridData(SWT.LEFT,SWT.CENTER,false,false); gd_idText.widthHint = 100; idText.setData(gd_idText); final Label userNameLabel = new Label(group,SWT.NONE); final GridData gd_userNameLabel = new GridData(SWT.LEFT,SWT.CENTER,false,false); userNameLabel.setText("UserName:"); userNameLabel.setData(gd_userNameLabel); final Text userNameText = new Text(group,SWT.BORDER); final GridData gd_userNameText = new GridData(SWT.LEFT,SWT.CENTER,false,false); gd_userNameText.widthHint = 100; userNameText.setData(gd_userNameText); final Label sexLabel = new Label(group,SWT.NONE); final GridData gd_sexLabel = new GridData(SWT.LEFT,SWT.CENTER,false,false); sexLabel.setText("Sex"); sexLabel.setData(gd_sexLabel); final Text sexText = new Text(group,SWT.BORDER); final GridData gd_sexText = new GridData(SWT.LEFT,SWT.CENTER,false,false); gd_sexText.widthHint = 100; sexText.setData(gd_sexText); Button searchButton = new Button(group,SWT.NONE); final GridData gd_searchButton = new GridData(SWT.CENTER,SWT.CENTER,false,false); searchButton.setData(gd_searchButton); searchButton.setText("Search"); searchButton.addMouseListener(new MouseListener(){ public void mouseDoubleClick(MouseEvent e) { } public void mouseDown(MouseEvent e) { //调用hessian服务,获取数据 ApplicationContext context = new ClassPathXmlApplicationContext("hessian-client.xml"); IHessianService service = (IHessianService)context.getBean("hessianService"); String value = idText.getText(); int id = Integer.parseInt(value); User user = service.getUserById(id); if(null!=user){ userNameText.setText(user.getUserName()); sexText.setText(user.getSex()); } else { MessageBox box = new MessageBox(shell); box.setText("Tip"); box.setMessage("User Not Found"); box.open(); } } public void mouseUp(MouseEvent e) { } }); shell.open(); while(!shell.isDisposed()){ if(!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } }
到这里整个DEMO项目就完成了。如果输入不存在的,则提示: