一、hessian的maven信息:
[html] view plain copy
print
?
- <dependency>
- <groupId>com.caucho</groupId>
- <artifactId>hessian</artifactId>
- <version>4.0.38</version>
- </dependency>
二、入门示例,以web方式提供对外接口
(1)服务接口:
[java] view plain copy
print
?
- package com.tianjunwei.hessian.server;
- public interface HelloService {
- public String helloWorld(String message);
- }
(2)接口实现:
[java] view plain copy
print
?
- package com.tianjunwei.hessian.server;
- public class HelloServiceImpl implements HelloService{
- @Override
- public String helloWorld(String message) {
- return "hello," + message;
- }
- }
(3)web.xml的配置:
[html] view plain copy
print
- <web-app>
- <servlet>
- <servlet-name>hessian-service</servlet-name>
- <servlet-class>
- com.caucho.hessian.server.HessianServlet
- </servlet-class>
- <init-param>
- <param-name>home-class</param-name>
- <param-value>
- <!-- 服务实现类 -->
- com.tianjunwei.hessian.server.HelloServiceImpl
- </param-value>
- </init-param>
- <init-param>
- <param-name>home-api</param-name>
- <!-- 服务接口 -->
- <param-value>com.tianjunwei.hessian.server.HelloService</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>hessian-service</servlet-name>
- <url-pattern>/hessian</url-pattern>
- </servlet-mapping>
- </web-app>
(4)客户端调用:
第(3)步中,通过配置servlet,这样就可以通过发布的http进行访问,根据Hessian提供的机制,这样就可以通过url进行服务调用。
[java] view plain copy
print
?
- package com.tianjunwei.hessian.client;
- import java.net.MalformedURLException;
- import com.caucho.hessian.client.HessianProxyFactory;
- import com.tianjunwei.hessian.server.HelloService;
- public class HelloServiceMain {
- public static void main(String [] args) throws MalformedURLException{
- "http://localhost:8080/hessian";
- System.out.println(url);
- new HessianProxyFactory();
- class, url);
- "jimmy"));
- }
- }
运行结果:
http://localhost:8080/hessianhello,jimmy
标签:入门,示例,server,Hessian,tianjunwei,com,public,hessian,String From: https://blog.51cto.com/tiantianzaixian/5953858