-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e452008
Showing
9 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
## Spring 与 Hessian 整合 | ||
demo采用的框架版本较高,jdk8, servlet-3.1, spring-5.0.0.RELEASE, Hessian-4.0.38, tomcat-8.5.23,详见pom.xml | ||
|
||
### web.xml中的配置 | ||
<servlet> | ||
<servlet-name>remote</servlet-name> | ||
<!-- 使用Spring的代理Servlet --> | ||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> | ||
<init-param> | ||
<param-name>namespace</param-name> | ||
<param-value>classes/remote-servlet</param-value> | ||
</init-param> | ||
<load-on-startup>1</load-on-startup> | ||
</servlet> | ||
|
||
<servlet-mapping> | ||
<servlet-name>remote</servlet-name> | ||
<url-pattern>/remote/*</url-pattern> | ||
</servlet-mapping> | ||
|
||
### remote-servlet.xml 配置 | ||
<beans> | ||
<!-- 接口的具体实现类 --> | ||
<bean id="helloService" class="com.eastrobot.service.HelloServiceImpl" /> | ||
<!-- 使用Spring的HessianServie做代理 --> | ||
<bean name="/hello" class="org.springframework.remoting.caucho.HessianServiceExporter"> | ||
<!-- service引用具体的实现实体Bean--> | ||
<property name="service" ref="helloService" /> | ||
<property name="serviceInterface" value="com.eastrobot.service.HelloService" /> | ||
</bean> | ||
<!-- 可以配置多个HessianServiceExporter代理Bean --> | ||
</beans> | ||
|
||
### 测试用例 | ||
com.eastrobot.service.HelloServiceTests | ||
|
||
访问地址:http://localhost:8080/kbase-hessian/remote/hello | ||
|
||
### 遇到的报错 | ||
* org.springframework.web.util.NestedServletException: Hessian skeleton invocation failed; nested exception is java.io.IOException: Expected 'H'/'C' (Hessian 2.0) or 'c' (Hessian 1.0) in hessian input at -1 | ||
|
||
**解决方法**:查阅很多资料后,mvn 重新 clean package 后就好了 | ||
|
||
### 参考文档 | ||
|
||
http://hessian.caucho.com/doc/hessian-overview.xtp#HessianClient | ||
|
||
http://blog.csdn.net/dengdaixing/article/details/52250853 | ||
|
||
http://www.cnblogs.com/yeahwell/p/4684492.html | ||
|
||
http://lavasoft.blog.51cto.com/62575/191889/ | ||
|
||
### Hessian使用过程中可能遇到的问题 | ||
* 实体类需要实现 Serializable 接口 | ||
* 复杂对象的传输效率问题 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<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.eastrobot</groupId> | ||
<artifactId>kbase-hessian</artifactId> | ||
<packaging>war</packaging> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>kbase-hessian Maven Webapp</name> | ||
<url>http://maven.apache.org</url> | ||
<dependencies> | ||
|
||
<!-- https://mvnrepository.com/artifact/com.caucho/hessian --> | ||
<dependency> | ||
<groupId>com.caucho</groupId> | ||
<artifactId>hessian</artifactId> | ||
<version>4.0.38</version> | ||
</dependency> | ||
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-core</artifactId> | ||
<version>5.0.0.RELEASE</version> | ||
</dependency> | ||
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-web</artifactId> | ||
<version>5.0.0.RELEASE</version> | ||
</dependency> | ||
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-webmvc</artifactId> | ||
<version>5.0.0.RELEASE</version> | ||
</dependency> | ||
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpclient</artifactId> | ||
<version>4.5.3</version> | ||
</dependency> | ||
|
||
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<version>3.1.0</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.10</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<finalName>kbase-hessian</finalName> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Power by www.xiaoi.com | ||
*/ | ||
package com.eastrobot.service; | ||
/** | ||
* @author <a href="mailto:[email protected]">eko.zhan</a> | ||
* @date 2017年10月16日 下午4:46:07 | ||
* @version 1.0 | ||
*/ | ||
public interface HelloService { | ||
|
||
public String sayHello(); | ||
|
||
public String sayHi(String username); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Power by www.xiaoi.com | ||
*/ | ||
package com.eastrobot.service; | ||
|
||
import com.caucho.hessian.server.HessianServlet; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">eko.zhan</a> | ||
* @date 2017年10月16日 下午4:54:49 | ||
* @version 1.0 | ||
*/ | ||
public class HelloServiceImpl extends HessianServlet implements HelloService { | ||
|
||
public String sayHello() { | ||
return "Hello World"; | ||
} | ||
|
||
public String sayHi(String username){ | ||
return "Hi " + username; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> | ||
<beans> | ||
<!-- 接口的具体实现类 --> | ||
<bean id="helloService" class="com.eastrobot.service.HelloServiceImpl" /> | ||
<!-- 使用Spring的HessianServie做代理 --> | ||
<bean name="/hello" class="org.springframework.remoting.caucho.HessianServiceExporter"> | ||
<!-- service引用具体的实现实体Bean--> | ||
<property name="service" ref="helloService" /> | ||
<property name="serviceInterface" value="com.eastrobot.service.HelloService" /> | ||
</bean> | ||
|
||
<!-- 可以配置多个HessianServiceExporter代理Bean --> | ||
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!DOCTYPE web-app PUBLIC | ||
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" | ||
"http://java.sun.com/dtd/web-app_2_3.dtd" > | ||
|
||
<web-app> | ||
<display-name>kbase-hessian server</display-name> | ||
<servlet> | ||
<servlet-name>remote</servlet-name> | ||
<!-- 使用Spring的代理Servlet --> | ||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> | ||
<init-param> | ||
<param-name>namespace</param-name> | ||
<param-value>classes/remote-servlet</param-value> | ||
</init-param> | ||
<load-on-startup>1</load-on-startup> | ||
</servlet> | ||
|
||
<servlet-mapping> | ||
<servlet-name>remote</servlet-name> | ||
<url-pattern>/remote/*</url-pattern> | ||
</servlet-mapping> | ||
</web-app> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!DOCTYPE HTML> | ||
<html> | ||
<head> | ||
<title>Hello Hessian</title> | ||
</head> | ||
<body> | ||
Hello Hessian! | ||
</body> | ||
</html> |
31 changes: 31 additions & 0 deletions
31
src/test/java/com/eastrobot/service/HelloServiceTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Power by www.xiaoi.com | ||
*/ | ||
package com.eastrobot.service; | ||
|
||
import java.net.MalformedURLException; | ||
|
||
import org.junit.Test; | ||
|
||
import com.caucho.hessian.client.HessianProxyFactory; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">eko.zhan</a> | ||
* @date 2017年10月16日 下午6:15:38 | ||
* @version 1.0 | ||
*/ | ||
public class HelloServiceTests { | ||
|
||
@Test | ||
public void sayHello() throws MalformedURLException{ | ||
String url = "http://localhost:8080/kbase-hessian/remote/hello"; | ||
HessianProxyFactory factory = new HessianProxyFactory(); | ||
factory.setOverloadEnabled(true); | ||
HelloService api = (HelloService) factory.create(HelloService.class, url); | ||
|
||
System.out.println(api.sayHello()); | ||
System.out.println("---------------------------"); | ||
|
||
System.out.println(api.sayHi("eko.zhan")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Power by www.xiaoi.com | ||
*/ | ||
package com.eastrobot.service; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.http.HttpEntity; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.ClientProtocolException; | ||
import org.apache.http.client.ResponseHandler; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.apache.http.util.EntityUtils; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">eko.zhan</a> | ||
* @date 2017年10月16日 下午7:21:49 | ||
* @version 1.0 | ||
*/ | ||
public class HelloWorld { | ||
|
||
public static void main(String[] args) throws ClientProtocolException, IOException { | ||
System.out.println(1); | ||
CloseableHttpClient httpclient = HttpClients.createDefault(); | ||
try { | ||
HttpPost httpget = new HttpPost("http://localhost:8080/kbase-hessian/remote/hello"); | ||
|
||
System.out.println("Executing request " + httpget.getRequestLine()); | ||
|
||
// Create a custom response handler | ||
ResponseHandler<String> responseHandler = new ResponseHandler<String>() { | ||
|
||
public String handleResponse( | ||
final HttpResponse response) throws ClientProtocolException, IOException { | ||
int status = response.getStatusLine().getStatusCode(); | ||
if (status >= 200 && status < 300) { | ||
HttpEntity entity = response.getEntity(); | ||
return entity != null ? EntityUtils.toString(entity) : null; | ||
} else { | ||
throw new ClientProtocolException("Unexpected response status: " + status); | ||
} | ||
} | ||
|
||
}; | ||
String responseBody = httpclient.execute(httpget, responseHandler); | ||
System.out.println("----------------------------------------"); | ||
System.out.println(responseBody); | ||
} finally { | ||
httpclient.close(); | ||
} | ||
} | ||
} |