当前位置:编程学习 > JAVA >>

spring integration之http-rest例子解析

spring integration例子代码下载地址,以及如何导入eclipse并使用maven构建等请参看上一篇:

Spring Integration实例代码分析之basic--http

先看一下http-rest例子的readme
This sample demonstrates how you can send an HTTP request to a Spring Integration's HTTP service while utilizing Spring Integration's new HTTP Path usage;
This sample also uses Spring Security for HTTP Basic authentication. With HTTP Path facility, the client program can send requests with URL Variables.
It consists of two parts - Client and Server.
The following client program can be used to test the HTTP Path usage.
1. RestHttpClientTest. It uses Spring's RestTemplate to assemble and send HTTP request
Server is Spring Integration's HTTP endpoint configuration.

此示例演示了如何发送一个HTTP请求到一个Spring集成的HTTP服务,同时利用Spring Integration的新的HTTP Path;
此示例还使用了Spring HTTP基本身份验证安全性。 HTTP Path,客户端程序可以发送请求的URL变量。
它由两部分组成 - 客户端和服务器。
客户端程序,可以用来测试使用HTTP path。RestHttpClientTest。它使用Spring的RestTemplate的组装和发送HTTP请求
服务端是Spring集成的HTTP端点配置。
代码结构

 

仍然先看web.xml
先是配置几个变量,指定spring配置路径和log4j的配置路径
[html]
      <context-param> 
       <param-name>contextConfigLocation</param-name> 
       <param-value> 
            <!-- Spring application context declaration --> 
             /WEB-INF/config/web-application-config.xml                
       </param-value> 
    </context-param> 
     
    <!-- 
Key of the system property that should specify the root directory of this 
web app. Applied by WebAppRootListener or Log4jConfigListener. 
--> 
<context-param> 
    <param-name>webAppRootKey</param-name> 
    <param-value>rest-http.root</param-value> 
</context-param> 
  
    <!-- 
Location of the Log4J config file, for initialization and refresh checks. 
Applied by Log4jConfigListener. 
--> 
<context-param> 
    <param-name>log4jConfigLocation</param-name> 
    <param-value>/WEB-INF/classes/log4j.properties</param-value> 
</context-param> 

然后配置spring的listener,用于启动WebApplicationContext
[html] 
<listener> 
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
</listener> 
  
   <!-- 
    - Loads the root application context of this web app at startup, 
    - by default from "/WEB-INF/applicationContext.xml". 
    - Note that you need to fall back to Spring's ContextLoaderServlet for 
    - J2EE servers that do not follow the Servlet 2.4 initialization order. 
    - 
    - Use WebApplicationContextUtils.getWebApplicationContext(servletContext) 
    - to access it anywhere in the web application, outside of the framework. 
    - 
    - The root context is the parent of all servlet-specific contexts. 
    - This means that its beans are automatically available in these child contexts, 
    - both for getBean(name) calls and (external) bean references. 
--> 
    <listener> 
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

接着是filter,包含指定字符的,和security的,用于认证。
[html] 
<filter> 
            <filter-name>charEncodingFilter</filter-name> 
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
            <init-param> 
              <param-name>encoding</param-name> 
              <param-value>UTF-8</param-value> 
            </init-param> 
            <init-param> 
              <param-name>forceEncoding</param-name> 
              <param-value>true</param-value> 
            </init-param> 
        </filter> 
        <filter-mapping> 
            <filter-name>charEncodingFilter</filter-name> 
            <url-pattern>/*</url-pattern> 
        </filter-mapping> 
        <filter> 
            <filter-name>springSecurityFilterChain</filter-name> 
            <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
        </filter> 
        <filter-mapping> 
            <filter-name>springSecurityFilterChain</filter-name> 
            <url-pattern>/*</url-pattern> 
        </filter-mapping> 

最后才是用于spring mvc的DispatcherServlet,指定配置文件为contextConfigLocation的 /WEB-INF/config/web-application-config.xml
[html] 
<servlet> 
            <servlet-name>Spring Integration Rest HTTP Path Usage</servlet-name> 
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
            <init-param> 
       &nb

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,