java系统之间的接口通信1

本文介绍了如何在Java中使用Spring和Hessian实现系统间的接口通信。通过在web.xml配置HessianServerServlet,编写Servlet来处理请求,并在Spring XML配置文件中定义服务和接口实现,以及展示CmiService接口及其Hessian实现类的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

处理方式:

 接口主方

  (1)在web.xml中配置相应的servlet 用以处理所有的请求

   <servlet>
     <servlet-name>hessianServerServlet</servlet-name>
     <servlet-class>com.psychcn.cmi3.web.servlet.HessianServerServlet</servlet-class>
  
   </servlet>
  
   <servlet-mapping>
     <servlet-name>hessianServerServlet</servlet-name>
     <url-pattern>/hessian/*</url-pattern>
   </servlet-mapping>

 

编写servlet

import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.remoting.caucho.HessianServiceExporter;
import org.springframework.web.util.UrlPathHelper;

import com.psychcn.util.SpringUtils;

public class HessianServerServlet extends HttpServlet {

 private static final long serialVersionUID = -7579598882061647014L;
 
 ConcurrentMap<String, HessianServiceExporter> hessianExporterMappings =
  new ConcurrentHashMap<String, HessianServiceExporter>();
 
 UrlPathHelper urlPathHelper = new UrlPathHelper();
 
 private ApplicationContext ac = null;
 
 @Override
 protected void service(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  String requestPath = urlPathHelper.getPathWithinServletMapping(request);
  if (requestPath!= null && requestPath.length() > 1) {
   HessianServiceExporter exporter = hessianExporterMappings.get(requestPath);
   if (exporter != null) {
    exporter.handleRequest(request, response);
   } else {
    response.setCharacterEncoding("utf-8");
    response.getWriter().println("未找到相应的服务,请检查 Hessian URL 是否正确 (error url: " + requestPath + ")");
   }
  } else {
   response.setCharacterEncoding("utf-8");
   response.getWriter().println("请求的是根路径,请检查 Hessian URL 是否配置正确");
  }
  
 }
 
 @Override
 public void init(ServletConfig config) throws ServletException {
  this.ac = SpringUtils.getWebApplicationContext(config.getServletContext());
  if (this.ac != null) {
   // 将Spring容器中定义的 HessianServiceExporter 均缓存到本实例变量中
   this.hessianExporterMappings.putAll(ac.getBeansOfType(HessianServiceExporter.class));
  } else  {
   super.log("错误,Hessian 服务组件无法获取 Spring 上下文");
  }
 }
 
 @Override
 public void destroy() {
  ac = null;
  hessianExporterMappings.clear();
  super.destroy();
  
 }}

spring xml中的配置

 <bean id="cmiService" class="com.psychcn.cmi3.remoting.hessian.CmiServiceHessianImpl" >
  <constructor-arg index="0" ref="mainDataSource" />
 </bean>
 
 <bean name="/cmiService" class="org.springframework.remoting.caucho.HessianServiceExporter">
  <property name="service" ref="cmiService" />
  <property name="serviceInterface" value="com.psychcn.cmi3.remoting.CmiService" />
 </bean>

 

 

编写相应的接口即实现类:

public interface CmiService {}

public class CmiServiceHessianImpl extends ServiceImpl  implements CmiService, InitializingBean, ApplicationContextAware {}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值