zoukankan      html  css  js  c++  java
  • 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());
    	}
    }
    


  • 相关阅读:
    OK335xS-Android mkmmc-android-ubifs.sh hacking
    OK335xS-Android pack-ubi-256M.sh hacking
    OK335xS Ubuntu 12.04.1 版本 Android 开发环境搭建
    Qt Quick Hello World hacking
    Qt QML referenceexamples attached Demo hacking
    QT 5.4.1 for Android Ubuntu QtWebView Demo
    I.MX6 working note for high efficiency
    QT 5.4.1 for Android Windows环境搭建
    mkbootimg hacking
    Generate And Play A Tone In Android hacking
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/6772788.html
Copyright © 2011-2022 走看看