《pro Spring》学习笔记之Spring HTTP 远程方法调用返回复杂类型的方法

来源:互联网 发布:ai人工智能需要学什么 编辑:程序博客网 时间:2024/05/18 10:38

相比使用Axis,使用Spring HTTP方式调用返回复杂类型的方法要简单的多,甚至可以说和普通类型没有任何区别,唯一要注意的就是我们的javaBean要实现序列化接口

服务端:

JavaBean:

 

package ch16.ComplexHTTP;

import java.io.Serializable;

public class MessageBean implements Serializable {
   
private String message;
   
private String senderName;
   
public MessageBean(){
       
   }

public MessageBean(String message, String senderName) {

    
this.message = message;
    
this.senderName = senderName;
}

public String getMessage() {
    
return message;
}

public void setMessage(String message) {
    
this.message = message;
}

public String getSenderName() {
    
return senderName;
}

public void setSenderName(String senderName) {
    
this.senderName = senderName;
}

public String toString(){
    
return this.message+" by "+this.senderName;
}

}

 服务接口:

 

package ch16.ComplexHTTP;

public interface MessageService {
   
public MessageBean getMessageBean();
}

 

服务实现:

 

package ch16.ComplexHTTP;

public class SimpleMessageService implements MessageService {

    
public MessageBean getMessageBean() {
        
return new MessageBean("helloworld","gaoxiang");
    }


}

 

配置文件httpInvoker-servlet.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans
    
xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
</bean>

<bean name="/message" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
  
<property name="service">
    
<bean class="ch16.ComplexHTTP.SimpleMessageService"/>
  
</property>
  
<property name="serviceInterface">
    
<value>ch16.ComplexHTTP.MessageService</value>
  
</property>
</bean>
</beans>

 

web.xml

 

<context-param>
      
<param-name>contextConfigLocation</param-name>
      
<param-value>/WEB-INF/httpInvoker-servlet.xml</param-value>
</context-param>
 
<servlet>
      
<servlet-name>httpInvoker</servlet-name>
      
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      
<load-on-startup>3</load-on-startup>
    
</servlet>
  
   
<servlet-mapping>
    
<servlet-name>httpInvoker</servlet-name>
    
<url-pattern>/http/*</url-pattern>
  
</servlet-mapping>

 

客户端:

首先把MessageService接口和MessageBean打包成jar,放到客户端工程的classpath下

配置文件的

 

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns
="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="messageBeanService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
  
<property name="serviceUrl">
    
<value>http://localhost:81/ProSpringStudyWeb/http/message</value>
  </property>
  
<property name="serviceInterface">
    
<value>ch16.ComplexHTTP.MessageService</value>
  
</property>
</bean>
</beans>

 

测试代码:

 

package ch16.ComplexHTTP;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;



public class Test {

    
public static void main(String[] args) {
        ApplicationContext context 
= new ClassPathXmlApplicationContext("ch16/ComplexHTTP/applicationContext-client.xml");
        MessageService messageService
=(MessageService)context.getBean("messageBeanService");
        System.out.println(messageService.getMessageBean());
    
    }


}

 

运行结果:

helloWorld by gaoxiang

原创粉丝点击