第一步骤:引入jar包,hessian-4.0.6.jar
第二步骤:在我们项目中web.xml,配置的拦截器是,过滤的是.ik路径,所以我们为了不让拦截器拦截,我们在添加一个后缀,放过。
<filter>
<filter-name>uriFilter</filter-name>
<filter-class>com.cntaiping.life.interceptor.SessionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>uriFilter</filter-name>
<url-pattern>*.ik</url-pattern>
</filter-mapping>
修改下面配置
<!-- servlet控制跳转 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml,classpath:sys-servlet.xml</param-value>--新添加的配置文件
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.ik</url-pattern>
<url-pattern>*.hessian</url-pattern> ----新增的,拦截器不拦截
</servlet-mapping>
第二步骤:classpath:sys-servlet.xml 配置文件内容
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<!--hessian-->
<bean id="httpRequestHandlerAdapter" class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean id="hessianCustomerServiceImpl" class="com.cntaiping.life.logic.impl.HessianCustomerServiceImpl"/>--实现类路径
<bean name="/getPresentGradeByUserId.hessian" class="org.springframework.remoting.caucho.HessianServiceExporter">--暴露给外部访问的路径
<property name="service" ref="hessianCustomerServiceImpl"/>--要暴露的实现类
<property name="serviceInterface" value="com.cntaiping.life.logic.HessianCustomerService"/>---要暴露的接口
</bean>
</beans>
HessianCustomerService.java
package com.cntaiping.life.logic;
public interface HessianCustomerService {
public String getPresentGradeByUserId(String user_id);
}
HessianCustomerServiceImpl.java
package com.cntaiping.life.logic.impl;
import org.springframework.beans.factory.annotation.Autowired;
import com.cntaiping.life.dao.HessianCustomerMapper;
import com.cntaiping.life.logic.HessianCustomerService;
public class HessianCustomerServiceImpl implements HessianCustomerService{
@Autowired
HessianCustomerMapper hessianCustomerMapper;
@Override
public String getPresentGradeByUserId(String user_id){
String presentGrade = hessianCustomerMapper.getPresentGradeByUserId(user_id);
return presentGrade;
}
}
HessianCustomerMapper.java
package com.cntaiping.life.dao;
public interface HessianCustomerMapper {
public String getPresentGradeByUserId(String user_id);
}
HessianCustomerMapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.cntaiping.life.dao.HessianCustomerMapper">
<select id="getPresentGradeByUserId" resultType="String" parameterType="String">
select *
from (Select s.present_grade,
row_number() over(partition by s.user_id order by bat desc) rn
from t_sco s where s.user_id =#{user_id} )
where rn = 1
</select>
</mapper>
第三步骤:启动项目后,我们在搞一个测试项目用来测试暴露的服务是否可以正常访问,测试项目中一定要有一个一样的接口类(这里就是我们的HessianCustomerService.java )
测试类:TestClient.java
package com.session.client;
import java.net.MalformedURLException;
import com.caucho.hessian.client.HessianProxyFactory;
import com.hession.service.HessianCustomerService;
import com.hession.service.IPrinter;
public class TestClient {
/**
* @param args
* @throws MalformedURLException
*/
public static void main(String[] args) throws MalformedURLException {
String url = "http://127.0.0.1:8080/amltpDemo/getPresentGradeByUserId.hessian";
HessianProxyFactory factory = new HessianProxyFactory();
factory.setOverloadEnabled(true);
HessianCustomerService hessianCustomerService = (HessianCustomerService) factory.create(HessianCustomerService.class, url);
String aaa = hessianCustomerService.getPresentGradeByUserId("211777078");
System.out.println(aaa);
}
}
HessianCustomerMapper.java
package com.cntaiping.life.dao;
public interface HessianCustomerMapper {
public String getPresentGradeByUserId(String user_id);
}
第四步骤:启动测试项目,完成测试。