jetty hessian 整合

先寫一個interface:json

import com.alibaba.fastjson.JSONObject;

public interface Hello {

    String sayHello(String name);

    JSONObject findHero(String name);

}

寫一個interface的實現類:eclipse

import com.alibaba.fastjson.JSONObject;
import com.caucho.hessian.server.HessianServlet;

public class HelloImpl extends HessianServlet implements Hello {

    private static final long serialVersionUID = 1464625224364842441L;

    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }

    @Override
    public JSONObject findHero(String name) {
        JSONObject hero = new JSONObject();
        hero.put("name", name);
        hero.put("age", "76");
        return hero;
    }
}

將這個實現類加到jettyserver中:ide

  1. import org.eclipse.jetty.server.Server;  
  2. import org.eclipse.jetty.servlet.ServletContextHandler;  
  3. import org.eclipse.jetty.servlet.ServletHolder;  
  4.   
  5. public class HessianTest {  
  6.   
  7. public static void main(String[] args) throws Exception {  
  8. Server server = new Server(3389);  
  9. ServletContextHandler context = new ServletContextHandler(  
  10. ServletContextHandler.SESSIONS);  
  11. server.setHandler(context);  
  12. ServletHolder servletHolder = new ServletHolder(new HelloImpl());  
  13. context.addServlet(servletHolder, "/hello");  
  14. server.start();  
  15. server.join();  
  16. }  
  17.   

運行HessianTest,此時,jettyserver就啓動了,而後再寫個測試的client:測試

public class TestApp {

    public static void main(String[] args) {
        try {
            HessianProxyFactory factory = new HessianProxyFactory();

            String url = "http://localhost:3389/hello";

            Hello us = (Hello)factory.create(Hello.class,url);
            String jsonObject = us.sayHello("123");
            System.out.println(jsonObject);
            JSONObject hero = us.findHero("張飛");
            System.out.println(hero);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

返回結果:url

Hello 123
{"name":"張飛","age":"76"}server

表示成功!!!ip

相關文章
相關標籤/搜索