相比使用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
本文通过实例演示了如何使用Spring的HTTP Invoker进行远程服务调用,并返回复杂类型的对象。详细介绍了服务端与客户端的配置步骤,包括JavaBean定义、服务接口及实现类创建、配置文件设置等。
1099

被折叠的 条评论
为什么被折叠?



