hessian客户端调用服务端测试类
1. pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
2. 服务端
2-1 接口类
package hessian.server;
public interface MessageHessianService {
/**
* 通知更新用户状态
*/
String noticePasswordUpdate(String json);
}
2-2 接口实现类
package hessian.server;
public interface MessageHessianServiceImpl {
/**
* 通知更新用户状态
*/
public String noticePasswordUpdate(String json){
log.info("触发接口-通知更新用户状态:{}",json);
if(StringUtils.isEmpty(json)){
return "0";
}
return "1";
}
}
2-3 发布
package hessian.server.config;
import bynonco.com.shareehelper.service.MessageHessianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.caucho.HessianServiceExporter;
/**
* hessian作为服务端
* 需要暴露的接口
*/
@Configuration
public class HessianExporterConfig {
@Autowired
private MessageHessianService messageHessianService;
@Bean(name = "/server/messageHessianService.hessian")
public HessianServiceExporter messageHessianService() {
HessianServiceExporter exporter = new HessianServiceExporter();
exporter.setService(messageHessianService);
exporter.setServiceInterface(MessageHessianService.class);
return exporter;
}
}
2-3 过滤器拦截处理Header,可忽略
View Code
JwtAuthenticationToken类设计:
View Code
3,客户端
3-1 接口类- 必须和服务端接口设计一致
package hessian.client;
public interface MessageHessianService implements MessageHessianService {
/**
* 通知更新用户状态
*/
String noticePasswordUpdate(String json);
}
3-2 HessianProxyFactoryUtil 工厂类
package hessian.client;
import hessian.client.HessianClientProxyFactory;
import com.caucho.hessian.client.HessianProxyFactory;
import java.util.HashMap;
import java.util.Map;
public class HessianProxyFactoryUtil {
public final static String url = "http://localhost:8080/api/server/messageHessianService.hessian";
private final static HessianProxyFactory factory = new HessianProxyFactory();
private static Map<Class,Object> hessianBeans = new HashMap<>();
/**
* 获取调用端对象
* @param clazz 实体对象泛型
* @param <T>
* @return 业务对象
*/
public static <T> T getHessianClientBean(Class<T> clazz, String code) throws Exception{
factory.setOverloadEnabled(true);
String token = UnitTokenTest.createToken(code); // 该类自定义创建token,可忽略
HessianClientProxyFactory hessianConnectionFactory = new HessianClientProxyFactory(token);
hessianConnectionFactory.setHessianProxyFactory(factory);
factory.setConnectionFactory(hessianConnectionFactory);
T result = null;
if(!hessianBeans.containsKey(clazz)) {
result = (T)factory.create(clazz,url);
hessianBeans.put(clazz, result);
}else {
result = (T)hessianBeans.get(clazz);
}
return result;
}
/**
* 移除hessionClientBean
* @param clazz
*/
public static void removeHessianClientBean(Class clazz) {
if(hessianBeans.containsKey(clazz)) {
hessianBeans.remove(clazz);
}
}
}
3-3 测试连接
package hessian.client;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class MessageHessianClientTest {
/**
* 测试用户密码变更通知
*/
@Test
public void noticePasswordUpdate(){
String code = "EEEE";
String sid = "123456";
try {
MessageHessianService messageHessianService = HessianProxyFactoryUtil.getHessianClientBean(MessageHessianService.class,code);
String msg = messageHessianService.noticePasswordUpdate(sid);
ConsoleLogger.info("触发接口-通知更新用户状态:{}",sid);
Assert.assertTrue(msg.equals("1"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
4. 遇到问题及解决方法
提示异常:
com.caucho.hessian.io.HessianProtocolException: '' is an unknown code
客户端和服务端返回参数类型不一致,服务端为int,客户端为String.
解决方法:客户端和服务器接口类一致即可解决问题.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
保存失败,我的随笔包含红色框内容,不支持该字符,所以无法保存.