一、服务端
web.xml 配置
Spring配置文件中配置服务
配置文件的定义有两种做法
说明
二、客户端
hessian-config.xml
测试
定义为war工程,使用dispatcherServlet作为发布服务的servlet
配置dispatcherServlet的映射路径,制定服务url规则,对于匹配路径规则的url自动找到对应的服务类处理
Archetype Created Web Application
contextConfigLocation
classpath:spring/*.xml
org.springframework.web.context.ContextLoaderListener
hessian
org.springframework.web.servlet.DispatcherServlet
hessian
/hessian/*
配置具体的url映射的服务
1. 定义文件名称为 [servletname]-servlet.xml ,这里的参数servletname是指配置的dispatcherServlet的名称。
文件放在src/main/webapp/WEB-INF路径下, 这里定义了一个新的应用上下文,它是被ContextLoaderListener加载的上下文的子上下文,本案例采用的是这种方案。
2. 直接在resources文件夹下配置文件,将文件的位置配置到上面的contextConfigLocation参数上,交由ContextLoaderListener加载
1. BeanNameUrlHandlerMapping 是当bean的name为“/”开头定义为url请求
2. HttpRequestHandlerAdapter 在这里面非常关键,没有它可能会出现这种错误:
-- No adapter for handler Does your handler implement a supported interface like Controller ?
HttpRequestHandlerAdapter它的作用就是让spring-mvc放出权限给下面的Exporter自己处理整个HTTP 流程
3. HessianServiceExporter这个类是将服务发布出去,有两个属性,service是指定实现类的bean,serviceInterface则是配置为接口的全类名。
注意:展示的配置文件,本例中是采用第一种方式定义的,定义为hessian-servlet.xml,通过ref引用父上下文中的实现类的bean的,实现类的bean定义在contextConfigLocation定义的上下文中。
配置url http://ip:端口/上下文/hessian/* 和接口名,获取代理对象
package com.github.skosmalla.hello.world.spring.hessian;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by zhouy on 2018/12/4
*/
public class HessianClient {
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext("hessian-config.xml");
HelloWorld service = (HelloWorld) appContext.getBean("helloWorldService");
String welcomeMessage = service.welcome();
System.out.println(welcomeMessage);
PrintSomeThing pn = (PrintSomeThing) appContext.getBean("printService");
String str = pn.print();
System.out.println(str);
}
}
完整案例下载:https://download.csdn.net/download/ditto_zhou/10826678
参考: http://gogomarine.iteye.com/blog/741209
https://blog.csdn.net/isea533/article/details/45038779
https://blog.sandra-parsick.de/2012/11/12/spring-web-application-with-hessian-services-as-a-maven-project/