《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;
}
}
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();
}
public interface MessageService ...{
public MessageBean getMessageBean();
}
服务实现:
package ch16.ComplexHTTP;
public class SimpleMessageService implements MessageService ...{
public MessageBean getMessageBean() ...{
return new MessageBean("helloworld","gaoxiang");
}
}
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>
<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>
<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>
<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());
}
}
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
- 《pro Spring》学习笔记之Spring HTTP 远程方法调用返回复杂类型的方法
- 《pro Spring》学习笔记之Spring HTTP 远程方法调用
- 《pro Spring》学习笔记之Spring HTTP 远程方法调用集成Tomcat实现安全验证
- 《pro Spring》学习笔记之Spring+Axis1.4开发WebService实例之复杂类型处理
- 《pro Spring》学习笔记之使用Sprin+James邮件服务器发送复杂类型邮件
- 《Pro Spring》学习笔记之生命周期init初始化方法
- 《Pro Spring》学习笔记之Method Replacement(方法替换)
- Spring MVC学习:处理方法返回值的可选类型
- Spring MVC学习:处理方法返回值的可选类型
- Spring MVC学习之三(处理方法返回值的可选类型)
- Spring MVC学习之处理方法返回值的可选类型
- Spring MVC学习之三:处理方法返回值的可选类型
- Spring MVC学习之三:处理方法返回值的可选类型
- Spring MVC学习之三:处理方法返回值的可选类型
- Spring MVC学习之:处理方法返回值的可选类型
- Spring MVC学习之:处理方法返回值的可选类型
- 《Pro Spring》学习笔记之DestroySinglton()自动调用
- Spring Rmi远程方法调用
- SQL Server 服务器安装剖析
- 高手做的地形与光照非常强(转,但是不知道文章的出处了)
- Local workstation xampp/Eclipse PDT/xdebug on WinXP from start to end
- prototype.js1.5.0开发者手册 译文+添加注释(四)关于ajax类的
- MemProof
- 《pro Spring》学习笔记之Spring HTTP 远程方法调用返回复杂类型的方法
- Hibernate继承映射“Could not format discriminator”错误解决
- 初识Firebug 全文 — firebug的使用
- 好文:java初学者应该搞懂的六个问题
- 我的软件安装问题
- 一个面向接口编程的好例子
- AD590的引脚使用
- 什么是ghost server
- .NET邮件发送