Announcement Announcement Module
Collapse
No announcement yet.
http-inbound-adapter: How is view-name used? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • http-inbound-adapter: How is view-name used?

    Problem solved! See solution below:

    For some reason, I can't make the view-name work for inbound-channel-adapter in Spring Integration. How do you exactly used this.

    By the way the documentation is too stingy in explaining the usage of this feature. The closest I got is from this thread: http://forum.springsource.org/showth...r-view-problem

    I got a view resolver and dispatcher servlet. From my service I have two results: a valid result and an exception. Is it possible to return a view name that will map to a valid response and another view name that will map to an exception.

    How? Thanks

    Here are my configs:

    integration.xml
    Code:
    <http:inbound-channel-adapter channel="INBOUND-HTTP" name="/test" supported-methods="POST" 
    		view-name="plain" />
    
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
        		p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
    web.xml
    Code:
    <servlet>
    		<servlet-name>Multipart</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>/WEB-INF/applicationContext.xml</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    Last edited by skram; Oct 29th, 2011, 06:58 AM.

  • #2
    By the way I get a 404 whenever the view-name is specified. But when I removed it, I get the usual 200 Ok status.

    Comment


    • #3
      Problem solved

      It turns out you need to configure the web.xml correctly and declare an appropriate view resolver.

      For reference, I'm posting the solution here:

      Here's my web.xml
      Code:
      	<servlet>
      		<servlet-name>Multipart</servlet-name>
      		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      		<init-param>
      			<param-name>contextConfigLocation</param-name>
      			<param-value>/WEB-INF/applicationContext.xml</param-value>
      		</init-param>
      		<load-on-startup>1</load-on-startup>
      	</servlet>
      
      	<servlet-mapping>
      		<servlet-name>Multipart</servlet-name>
      		<url-pattern>/upload</url-pattern>
      	</servlet-mapping>
      
      	<servlet-mapping>
      		<servlet-name>Multipart</servlet-name>
      		<url-pattern>/plain</url-pattern>
      	</servlet-mapping>
      /upload - This is the entry point for our application
      /plain - This is the view returned by the application

      spring-integration.xml:
      Code:
      	<int:channel id="INBOUND-HTTP" />
      	<int:channel id="REPLY"/>
      
      	<http:inbound-gateway request-channel="INBOUND-HTTP" name="/upload" supported-methods="POST, GET" 
      		reply-channel="REPLY" view-name="plain" />
      	
      	<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" p:order="1">
      		<property name="defaultViews">
      			<list>
      				<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
      			</list>
      		</property>
      </bean>
      Alternatively, you can declare the http inbound gateway as:
      Code:
      	<bean id="httpInbound" class="org.springframework.integration.http.inbound.HttpRequestHandlingController" name="/upload">
      		<constructor-arg value="true" /> 
      		<property name="requestChannel" ref="INBOUND-HTTP" />
        		<property name="replyChannel" ref="REPLY" />
      		<property name="viewName" value="plain" />
      		<property name="supportedMethodNames">
      			<list>
      				<value>POST</value>
      			</list>
      		</property>
      	</bean>
      Notice the view-name (or viewName depending if you're using the standard bean or namespace support) corresponds to the servlet mapping. And we've declared a ContentNegotiatingViewResolver and MappingJacksonJsonView which will convert the final response as JSON view.

      If we want a JSP to be returned, we must declare an InternalResourceViewResolver
      Code:
      	<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
          		p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
      And make sure you have a plain.jsp declared in the WEB-INF/jsp folder. The name must match the view-name and the servlet-mapping URL pattern.

      I just wished that the Spring Integration reference was more transparent in explaining this feature. (By the way it took me 8 hours to figure this out starting from 8pm to 5am ...yep I didn't sleep until I was able to get this out).

      When I have the extra time, I will be uploading a tutorial about this to my blog at http://krams915.blogspot.com

      Comment


      • #4
        Thank you so much for the solution. The SI reference document lacks documentation in this regard. I have been badly looking for help and luckily I found this one.

        Comment


        • #5
          Code:
          <bean id="httpInbound" class="org.springframework.integration.http.inbound.HttpRequestHandlingController" name="/upload">
          		<constructor-arg value="true" /> 
          		<property name="requestChannel" ref="INBOUND-HTTP" />
            		<property name="replyChannel" ref="REPLY" />
          		<property name="viewName" value="plain" />
          		<property name="supportedMethodNames">
          			<list>
          				<value>POST</value>
          			</list>
          		</property>
          	</bean>

          in the above declaration
          Code:
          <constructor-arg value="true" />
          tells spring to expect reply and not to send the 200 response code. how do i do the same when i use the method below ? ??

          Code:
          <http:inbound-gateway request-channel="INBOUND-HTTP" name="/upload" supported-methods="POST, GET" 
          		reply-channel="REPLY" view-name="plain" />

          i wish the spring documentation had consistent examples for each topic and i am new to this.

          Comment


          • #6
            The namespace support is a higher-level way of configuring the underlying beans (think of it as a domain-specific language where the "domain" is EIP). As such, there is not always a one-to-one mapping between the elements and classes OR a one-to-one mapping between attributes and properties. Sometimes the *presence* of a particular type of element drives underlying property values, and that is the case here...

            The use of "inbound-gateway" does indicate that it should expect a reply while "inbound-channel-adapter" indicates that it does NOT expect a reply.

            With Spring Integration adapters, "gateway" always means request/reply (bidirectional) while "channel-adapter" means one-way either inbound OR outbound (unidirectional).

            Hope that helps.
            -Mark

            Comment


            • #7
              Code:
              <http:inbound-channel-adapter channel="INBOUND-HTTP" name="/test" supported-methods="POST" 
              		view-name="plain" />
              i used the above declaration for the gateway

              this is the flow gateway->service-acitvator (injects a id)->splitter->router->aggregator-> return to client

              all i get his the response code, no content. i am just pushing a string to the channel, is that enough?

              Comment


              • #8
                Originally posted by ejkp View Post
                Code:
                <http:inbound-channel-adapter channel="INBOUND-HTTP" name="/test" supported-methods="POST" 
                		view-name="plain" />
                i used the above declaration for the gateway

                this is the flow gateway->service-acitvator (injects a id)->splitter->router->aggregator-> return to client

                all i get his the response code, no content. i am just pushing a string to the channel, is that enough?

                Since you are stating you want to use a "gateway" to return a response, you should be using:
                int-http:inbound-gateway instead of http:inbound-channel-adapter.

                Comment

                Working...
                X