maven + hessian 简单样例
2024-08-29 06:21:22
项目结构例如以下:
pom.xml 内容:
<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.lala</groupId>
<artifactId>hessian-web</artifactId>
<packaging>war</packaging>
<version>1.0.0</version> <name>hessian-web Maven Webapp</name>
<url>http://maven.apache.org</url> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency> </dependencies> <build>
<finalName>hessian-web</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<verbose>true</verbose>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.11.v20150529</version>
<configuration>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
<httpConnector>
<port>3389</port>
</httpConnector>
<webApp>
<contextPath>/</contextPath>
</webApp>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.lala.service; import java.util.Map; public interface UserService
{
public Map<String, Object> getById(long id); public String get();
}
package com.lala.service; import java.util.HashMap;
import java.util.Map; public class UserServiceImpl implements UserService
{
public String get()
{
return "get method invoke";
}
public Map<String, Object> getById(long id)
{
if(id <= 0)
{
return null;
}
Map<String, Object> result = new HashMap<>();
result.put("id", id);
result.put("name", "英布");
result.put("position", "九江王");
return result;
}
}
servlet例如以下:
package com.lala.servlet; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.caucho.hessian.server.HessianServlet; @WebServlet(value = "/hessian/user", initParams={@WebInitParam(name="service-class",value="com.lala.service.UserServiceImpl")})
public class UserHessianServlet extends HessianServlet
{
private static final long serialVersionUID = 1L; public void init() throws ServletException
{
super.init();
} protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
super.service(req, resp);
}
}
然后,启动jetty服务
mvn jetty:run
调用代码
package com.client; import com.caucho.hessian.client.HessianProxyFactory;
import com.lala.service.UserService; public class App
{
public static void main(String[] args)throws Exception
{
HessianProxyFactory factory = new HessianProxyFactory(); String url = "http://127.0.0.1:3389/hessian/user"; UserService us = (UserService)factory.create(UserService.class,url); System.out.println(us.getById(2l));
System.out.println(us.get());
}
}
最新文章
- git: fatal: Not a git repository (or any of the parent directories): .git
- C#中的?和??的用法
- Robot Framework--04 工作区
- Eclipse系列:如何断点调试web项目
- ASP.NET 中OAUTH 2.0 及OPENID CONNECT的介绍
- sublime text3的一些小技巧记录(配gif图)
- Unity3D--学习太空射击游戏制作(一)
- Sql 中常用日期转换Convert(Datetime)
- RAID的解释(附购买链接)
- Scala基础之注解(annotation
- linux yum命令 使用
- bzoj 5495
- Vnpy官网汇总
- Tensorflow之调试(Debug)及打印变量
- xsd操作
- C语言强化——排序
- AMQP协议与RabbitMQ、MQ消息队列的应用场景
- 在各OJ上的名号
- Oracle 数据库的绑定变量特性及应用
- 从官网学习Node.js FS模块方法速查
热门文章
- brew install memcache get Error: Formulae found in multiple taps
- .net core +codefirst(.net core 基础入门,适合这方面的小白阅读) 【我们一起写框架】领域驱动设计的CodeFirst框架(一)—序篇
- jQuery knowledge
- unity,List元素第一个成员最好是string类型
- 权限管理系统(四):RBAC权限模型分类介绍
- CConfig类
- Oracle 11g安装图文攻略
- Linux系统下Shell命令行快捷键实用技巧
- jquery 悬浮验证框架 jQuery Validation Engine
- DLL编写中extern “C”和__stdcall的作用