SpringHttpInvoker解析1-使用示例
Posted wade&luffy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringHttpInvoker解析1-使用示例相关的知识,希望对你有一定的参考价值。
HTTP invoker是一个新的远程调用模型,作为Spring框架的一部分,来执行基于HTTP的远程调用(让防火墙可以接受),并使用Java的序列化机制。
服务端
定义服务接口UserService
import java.util.List; import com.gosun.jws.daomain.Users; public interface UserService { public List<Users> geAlltUsers(); public Users getUser(String id); public void save(Users user); public void saves(List<Users> users); }
服务接口实现类
import java.util.List; import com.gosun.jws.daomain.Users; import com.gosun.jws.daomain.UsersFactory; public class UserServiceImpl implements UserService { @Override public List<Users> geAlltUsers() { return UsersFactory.getUsers(); } @Override public Users getUser(String id) { System.out.println(id); return UsersFactory.getUser(); } @Override public void save(Users user) { System.out.println(user.toJson()); } @Override public void saves(List<Users> users) { for (Users u : users) { System.out.println(u.toJson()); } } }
配置Spring的HttpInvokerServiceExporter和SimpleUrlHandlerMapping
<!-- 在Spring的httpInvoker服务 --> <bean id="httpInvokerUserService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <!--需要发布的实现类 --> <property name="service" ref="userServiceHttpInvoker" /> <property name="serviceInterface" value="com.gosun.jws.httpinvoker.UserService" /> </bean> <!-- 将特定的请求映射到具体的hessianservice --> <bean id="urlMapping1" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/httpInvokerUserService.service">httpInvokerUserService</prop> </props> </property> </bean> <bean id="userServiceHttpInvoker" class="com.gosun.jws.httpinvoker.UserServiceImpl" />
配置DispatcherServlet控制器
<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-httpinvoker.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.service</url-pattern> </servlet-mapping>
客户端
配置Spring的HttpInvokerProxyFactoryBean
<bean id="httpInvokerUserService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://localhost:8080/jws/httpInvokerUserService.service"/> <property name="serviceInterface" value="com.gosun.jws.httpinvoker.UserService" /> </bean>
测试代码
import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.gosun.jws.daomain.Users; public class ClientDemo { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("client-application/applicationContext-httpInvoker.xml"); UserService us = (UserService) ac.getBean("httpInvokerUserService"); Users user = us.getUser("a001"); System.out.println(user.toJson()); System.out.println("----------------------------------------"); List<Users> users = us.geAlltUsers(); for (Users u : users) { System.out.println(u.toJson()); } System.out.println("----------------------------------------"); us.save(user); System.out.println("----------------------------------------"); us.saves(users); }
以上是关于SpringHttpInvoker解析1-使用示例的主要内容,如果未能解决你的问题,请参考以下文章