【问题标题】:How to add REST channel to Spring Webflow Application?如何将 REST 通道添加到 Spring Webflow 应用程序?
【发布时间】:2012-11-08 09:03:57
【问题描述】:

我已经下载了 Spring Webflow 源,我想将 REST 通道(显示来自数据库的图像)添加到 jsf-booking 示例。此通道在我的 Spring-MVC 示例应用程序中正常工作。

但是,我的映射有问题,因为在给定地址上,JSF 处理程序正在响应错误消息,显示 display.xhtml 文件不存在(REST 侦听器应该处理该请求)。

这就是我注册处理程序的方式:

@Controller
@RequestMapping(value="/image")
public class ImageChannelImpl implements ImageChannel {

    @RequestMapping(value="/display.png", method=RequestMethod.GET)
    public void display(HttpServletResponse response) throws IOException {
(...)
        response.setContentType("image/png");
        ImageIO.write(img, "PNG", response.getOutputStream());
        response.getOutputStream().flush();     
        log.debug("display finished");
    }

}

我已经复制了处理程序映射;

<bean

    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <util:list id="beanList">
            <ref bean="stringHttpMessageConverter" />
            <ref bean="byteArrayHttpMessageConverter" />
        </util:list>
    </property>
</bean>

<bean id="stringHttpMessageConverter"
    class="org.springframework.http.converter.StringHttpMessageConverter" />

<bean id="byteArrayHttpMessageConverter"
    class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />

DispatcherServlet 正在监听 /spring/* 请求:

<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

<!-- Map all /spring requests to the Dispatcher Servlet for handling -->
<servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>/spring/*</url-pattern>
</servlet-mapping>

所以,我预计图像将在地址 /spring/image/display.png 上可用,但 REST 处理程序没有接受该请求,也没有处理 /image/display.png 。

那么,必须在该配置中进行哪些更改才能注册 REST 通道?我试图谷歌任何示例或文档,如何配置 Spring WebFlow 和 Spring MVC 通道,但我没有找到任何东西。

【问题讨论】:

    标签: java model-view-controller jsf rest spring-webflow


    【解决方案1】:

    您应该为 FlowHandlerMapping 定义“默认处理程序”:

    <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
        <property name="flowRegistry" ref="flowRegistry"/>
        <property name="order" value="0"/>
        <!--If no flow match, map path to a view to render; e.g. the "/intro" path would map to the view named "intro" --> 
        <property name="defaultHandler" ref="requestMappingHandlerMapping"/>
    </bean>
    
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" id="requestMappingHandlerMapping">
        <property name="order" value="1"/>
    </bean>
    

    您可以根据您的 Spring MVC 版本将 RequestMappingHandlerMapping 替换为 AnnotationMethodHandlerAdapter(RequestMappingHandlerMapping 是最新 SMVC 版本中的首选方式)

    【讨论】:

      猜你喜欢
      • 2021-04-24
      • 1970-01-01
      • 2016-01-21
      • 2017-08-25
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 2011-08-28
      • 1970-01-01
      相关资源
      最近更新 更多