Spring HTTP Service
2014-01-07 22:59
429 查看
简述:
基于Spring MVC, 使用Http Service Invoke远程调用方法
(参考: http://blog.csdn.net/hanqunfeng/article/details/4303127)
步骤:
1. 本地定义接口,并在配置文件中说明
PersonService.java
PersonService.java
applicationContext-server-http-service.xml
2. mvc配置服务uri与对应的service
applicationContext-mvc.xml
3. 客户端配置uri的invoke
applicationContext-client-http-service.xml
设置属性文件
system.properties
加载配置文件的xml
applicationContext-constants.xml
4. 编写测试方法
TestHttpServiceInvoke.java
输出:
基于Spring MVC, 使用Http Service Invoke远程调用方法
(参考: http://blog.csdn.net/hanqunfeng/article/details/4303127)
步骤:
1. 本地定义接口,并在配置文件中说明
PersonService.java
package com.anialy.httpservice.service; import com.anialy.httpservice.entity.Person; public interface PersonService { public Person getPersonByName(String name); }
PersonService.java
package com.anialy.httpservice.service.impl; import com.anialy.httpservice.entity.Person; import com.anialy.httpservice.service.PersonService; public class PersonServiceImpl implements PersonService{ public Person getPersonByName(String name) { if("anialy".equals(name)) return new Person("anialy", 100); return null; } }
applicationContext-server-http-service.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- 指定Spring配置文件的Schema信息 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="httpService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service"> <ref bean="personService" /> </property> <property name="serviceInterface" value="com.anialy.httpservice.service.PersonService"> </property> </bean> <bean id="personService" class="com.anialy.httpservice.service.impl.PersonServiceImpl"/> </beans>
2. mvc配置服务uri与对应的service
applicationContext-mvc.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <mvc:view-controller path="/" view-name="redirect:/home" /> <mvc:view-controller path="/home" view-name="home" /> <!-- Spring Service Invoke --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/service/httpService">httpService</prop> </props> </property> </bean> <!-- Spring MVC --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <value> /test=testController </value> </property> <property name="order" value="1" /> </bean> <bean id="testController" class="com.anialy.webproj.controller.TestController"> <property name="methodNameResolver" ref="paramResolver" /> </bean> <!-- 定义JSP --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="paramResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="action" /> <property name="defaultMethodName" value="test" /> </bean> </beans>
3. 客户端配置uri的invoke
applicationContext-client-http-service.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- 指定Spring配置文件的Schema信息 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="personService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean" depends-on="propertyConfigurer"> <property name="serviceUrl" > <value> http://${host}:${port}/${contextPath}/service/httpService </value> </property> <property name="serviceInterface" value="com.anialy.httpservice.service.PersonService"> </property> </bean> </beans>
设置属性文件
system.properties
serviceName=localhost host=127.0.0.1 port=8080 contextPath=WebProj
加载配置文件的xml
applicationContext-constants.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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> <value>classpath:system.properties</value> </list> </property> </bean> </beans>
4. 编写测试方法
TestHttpServiceInvoke.java
package httpservice; import java.io.IOException; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.anialy.httpservice.entity.Person; import com.anialy.httpservice.service.PersonService; public class TestHttpServiceInvoke { private static final Logger logger = LoggerFactory.getLogger(TestHttpServiceInvoke.class); private ApplicationContext ctx; private PersonService personService; @Before public void init() throws IOException{ ctx = new ClassPathXmlApplicationContext(new String[]{ "classpath*:/applicationContext-client-http-service.xml", "classpath*:/applicationContext-constants.xml" }); personService = (PersonService) ctx.getBean("personService"); System.out.println(""); } @Test public void test() { Person person = personService.getPersonByName("anialy"); logger.info("姓名:" + person.getName()); logger.info("年龄:" + person.getAge()); } }
输出:
相关文章推荐
- Spring + Shiro 项目 + HttpSessionListener 【调用springService问题】&【Session失效问题】
- RMI在Spring中的使用之HttpInvokerServiceExporter
- Spring HTTP Service
- HTTPSQS(HTTP Simple Queue Service)是一款基于 HTTP GET/POST 协议的轻量级开源简单消息队列服务
- Spring HttpInvoker 开发
- Spring注解@Component、@Repository、@Service、@Controller区别
- spring 4 异常HttpMediaTypeNotAcceptableException解决方案
- spring 如何将spring中的dao、service等注入到普通的Java类
- get与post区别详解,session与cookie区别,转发和重定向区别,如何实现session共享,webservice与httpservice区别
- Flex数据交互方法 :httpservice, webservice, RemoteObject, socket
- 性能优化之接口优化(spring/java/http接口)
- springMVC报WARNING: No mapping found for HTTP request with URI [/springMVC/hello] in DispatcherServle
- OSGi嵌入 http 服务器 时遇到的问题 缺少 org.apache.felix.service.command 包
- spring里的标注 @Service @Autowired
- 在servlet中如何使用被Spring管理的service
- ajax使用向Spring MVC发送JSON数据出现 org.springframework.web.HttpMediaTypeNotSupportedException: Content type
- SpringRMI解析3-RmiServiceExporter逻辑细节
- Spring Cloud云服务架构 - HongHu common-service 项目构建过程
- java Webservice(一)HttpClient使用(一)
- WebService 整合 Spring