maven + hessian 简单例子

项目结构如下:



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());
	}
}


  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值