如何在 spring 中封送对象并作为视图(而不是 JSP 页面)返回流媒体

发布于 2024-10-12 16:15:37 字数 4240 浏览 8 评论 0原文

我正在尝试按照本教程构建一个 RESTful 服务,该服务可以将对象解压缩到 XML 或从 XML 编组对象。 http://www.stupidjavatricks.com/?p=54

中选择的编组器文章是xStream(我发现它非常容易使用和配置)。

重点是,使用 STS(Spring 自己的 Eclipse 风格与 tcServer 捆绑在一起),我构建了一个基于 MVC 的 STS 模板的项目。这是一个从 Spring 2.4 版本开始的遗留项目,我将其迁移到 3.0 版本。因此,模板创建了所有必要的 XML 标记,并且我添加了配置以将视图指向正确的对象转换(指向 xstream 封送拆收器)。

这是我的 bean 的一部分,它将对象发送到新视图(从链接复制出来):

<bean id="bookXmlView" class="org.springframework.web.servlet.view.xml.MarshallingView">  
     <constructor-arg>  
         <bean class="org.springframework.oxm.xstream.XStreamMarshaller">  
             <property name="autodetectAnnotations" value="true"/>  
         </bean>  
     </constructor-arg>  
</bean>

一切都运行良好,直到我安装了最新的 STS 版本 2.5.2.RELEASE 并从模板创建了一个新的 MVC 项目。 (除了一些其他更改外,新模板不再使用 urlrewrite.xml)。

我按照教程的建议一一设置了正确的配置,但现在无论如何,视图始终定向到 JSP,因此如果我的控制器如下所示:

@RequestMapping(value = "/authors/{authorId}")  
public ModelAndView getAuthorById(@PathVariable String authorId) {  
    Author author = bookService.getAuthorById(authorId);  
    ModelAndView mav =new ModelAndView("bookXmlView", BindingResult.MODEL_KEY_PREFIX+"author", author);  
    return mav;  
}

它总是尝试返回到author.jsp 视图而不是XML 形式的对象。我尝试了很多事情但没有成功。有什么想法为什么会发生这种情况以及如何解决它吗?


更新-------------------------------- 如前所述,我添加了日志:

我将其设置为调试级别并发现了一些东西:

调试:org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - 拒绝 bean 名称“org.springframework.context.annotation.internalConfigurationAnnotationProcessor”:未识别 URL 路径
调试:org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - 拒绝bean名称“org.springframework.context.annotation.internalAutowiredAnnotationProcessor”:未识别URL路径
调试:org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - 拒绝bean名称“org.springframework.context.annotation.internalRequiredAnnotationProcessor”:未识别URL路径
调试:org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - 拒绝bean名称“org.springframework.context.annotation.internalCommonAnnotationProcessor”:未识别URL路径
调试:org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - 拒绝 bean 名称“bookXmlView”:未识别 URL 路径


注意这一行:拒绝的 bean 名称“bookXmlView”:未识别 URL 路径。 搜索此内容可能表明 和 xstream 设置中的 autodetectAnnotations 之间存在冲突?

无论如何,调用链接后,我得到了以下日志条目。请注意,它将视图转发到 /WEB-INF/views/bookXmlView.jsp:

DEBUG:org.springframework.web.servlet.DispatcherServlet - 名称为“appServlet”的 DispatcherServlet 处理 [/test/page_init]
的 GET 请求 调试:org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - 使用处理程序 [test.test.test.HomeController@a087de] 和 2 个拦截器将 [/page_init] 映射到 HandlerExecutionChain
调试:org.springframework.web.servlet.DispatcherServlet - [/test/page_init] 的上次修改值为:-1
DEBUG:org.springframework.web.bind.annotation.support.HandlerMethodInvoker - 调用请求处理程序方法:public org.springframework.web.servlet.ModelAndView test.test.test.HomeController.getObject()< br> 调试:org.springframework.beans.factory.support.DefaultListableBeanFactory - 对名称为“bookXmlView”的 bean 调用 afterPropertiesSet()
DEBUG: org.springframework.web.servlet.DispatcherServlet - 渲染视图 [org.springframework.web.servlet.view.JstlView: name 'bookXmlView'; DispatcherServlet 中名为“appServlet”的 URL [/WEB-INF/views/xmlView.jsp]]
DEBUG:org.springframework.web.servlet.view.JstlView - 添加了 [test.test.test.ObjectTest] 类型的模型对象“org.springframework.validation.BindingResult.books”以在视图中请求名称为“bookXmlView”
调试:org.springframework.web.servlet.view.JstlView - 转发到InternalResourceView“bookXmlView”中的资源[/WEB-INF/views/xmlView.jsp]
调试:org.springframework.web.servlet.DispatcherServlet - 成功完成请求

I'm trying to follow this tutorial and build a RESTful service that can un/marshal an object to/from XML.
http://www.stupidjavatricks.com/?p=54

The marshaller of choice in the article is xStream (I found it to be very easy to use and configure).

The point is, using STS -- Spring's own flavor of Eclipse bundled with tcServer -- I built a project based on the STS template of MVC. This is a legacy project started from Spring version 2.4, and I migrated it to version 3.0. So, the template created all the necessary XML markup, and I added my configuration to point the view to the correct object conversion (to the xstream marshaler).

Here is part of my bean that sends the object to a new view (copied out from the link):

<bean id="bookXmlView" class="org.springframework.web.servlet.view.xml.MarshallingView">  
     <constructor-arg>  
         <bean class="org.springframework.oxm.xstream.XStreamMarshaller">  
             <property name="autodetectAnnotations" value="true"/>  
         </bean>  
     </constructor-arg>  
</bean>

It all worked nicely until I installed the latest STS version 2.5.2.RELEASE and created a new MVC project from a template. (The new template does not use urlrewrite.xml anymore, among some other changes).

I set the correct configuration 1 by 1 as the tutorial suggests, but now no matter what, the view is always directed to a JSP, so if my controller looks like this:

@RequestMapping(value = "/authors/{authorId}")  
public ModelAndView getAuthorById(@PathVariable String authorId) {  
    Author author = bookService.getAuthorById(authorId);  
    ModelAndView mav =new ModelAndView("bookXmlView", BindingResult.MODEL_KEY_PREFIX+"author", author);  
    return mav;  
}

It would always try to return to a author.jsp view and not the object as XML. I tried many things with no success. Any ideas why this happens and how to fix it?


UPDATE -------------------
As noted I added logs:

I set it as DEBUG level and discovered something:

DEBUG: org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Rejected bean name 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor': no URL paths identified
DEBUG: org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Rejected bean name 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor': no URL paths identified
DEBUG: org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Rejected bean name 'org.springframework.context.annotation.internalRequiredAnnotationProcessor': no URL paths identified
DEBUG: org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Rejected bean name 'org.springframework.context.annotation.internalCommonAnnotationProcessor': no URL paths identified
DEBUG: org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Rejected bean name 'bookXmlView': no URL paths identified


Notice this line: Rejected bean name 'bookXmlView': no URL paths identified.
Searching this indicated maybe a clash between <mvc:annotation-driven /> and my autodetectAnnotations in the xstream settings?

Any case, after invoking the link, I got the following log entry. Notice it forwards the view to /WEB-INF/views/bookXmlView.jsp:

DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'appServlet' processing GET request for [/test/page_init]
DEBUG: org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapping [/page_init] to HandlerExecutionChain with handler [test.test.test.HomeController@a087de] and 2 interceptors
DEBUG: org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/test/page_init] is: -1
DEBUG: org.springframework.web.bind.annotation.support.HandlerMethodInvoker - Invoking request handler method: public org.springframework.web.servlet.ModelAndView test.test.test.HomeController.getObject()
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'bookXmlView'
DEBUG: org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'bookXmlView'; URL [/WEB-INF/views/xmlView.jsp]] in DispatcherServlet with name 'appServlet'
DEBUG: org.springframework.web.servlet.view.JstlView - Added model object 'org.springframework.validation.BindingResult.books' of type [test.test.test.ObjectTest] to request in view with name 'bookXmlView'
DEBUG: org.springframework.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/views/xmlView.jsp] in InternalResourceView 'bookXmlView'
DEBUG: org.springframework.web.servlet.DispatcherServlet - Successfully completed request

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屌丝范 2024-10-19 16:15:37

终于明白了!
首先,我尝试了不同的编组器 - JAXB2,但 xstream 应该也可以工作。

接下来是定义 - 结果由于某种原因配置使用了这个(错误):

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

仅使用 InternalResourceViewResolver

而定义 org.springframework.web.servlet.view.BeanNameViewResolver被忽略。解决方案是在一个名为 ContentNegotiatingViewResolver 的 bean 中定义它们,如下所示:

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="viewResolvers">
            <list>
                    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
                    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                            <property name="prefix" value="/WEB-INF/jsp/" />
                            <property name="suffix" value=".jsp" />
                    </bean>
            </list>
    </property>
</bean>
<oxm:jaxb2-marshaller id="marshaller">
        <oxm:class-to-be-bound name="com.mycompany.dashboard.Person" />
</oxm:jaxb2-marshaller>

<bean name="person" class="org.springframework.web.servlet.view.xml.MarshallingView">
        <constructor-arg ref="marshaller" />
</bean>

该配置解决了我的问题,并且我使用的 person 对象没有定向到 JSP 视图,而是编组器转向 XML:

    @RequestMapping(value = "/person", method = RequestMethod.GET)
public ModelAndView addPerson() {
    Person person = new Person();
    person.setAddress("address 123");
    person.setAge(50);
    person.setName("Andrew");
    System.out.println("new person: " + person);
    ModelAndView mav = new ModelAndView("person",BindingResult.MODEL_KEY_PREFIX + "person",person);
    return mav;

希望它将来也能帮助其他人。

Got it at last!
First I tried a different marshaller - JAXB2 but xstream should work as well.

Next thing is the definition - turns out for some reason the configuration uses this (wrong):

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

using only InternalResourceViewResolver

while definition for org.springframework.web.servlet.view.BeanNameViewResolver is ignored. The solution for that is to define them both in one bean called ContentNegotiatingViewResolver as follows:

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="viewResolvers">
            <list>
                    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
                    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                            <property name="prefix" value="/WEB-INF/jsp/" />
                            <property name="suffix" value=".jsp" />
                    </bean>
            </list>
    </property>
</bean>
<oxm:jaxb2-marshaller id="marshaller">
        <oxm:class-to-be-bound name="com.mycompany.dashboard.Person" />
</oxm:jaxb2-marshaller>

<bean name="person" class="org.springframework.web.servlet.view.xml.MarshallingView">
        <constructor-arg ref="marshaller" />
</bean>

That configuration solved my problems and person object I played with was not directed to a JSP view but marshaller turn is to an XML:

    @RequestMapping(value = "/person", method = RequestMethod.GET)
public ModelAndView addPerson() {
    Person person = new Person();
    person.setAddress("address 123");
    person.setAge(50);
    person.setName("Andrew");
    System.out.println("new person: " + person);
    ModelAndView mav = new ModelAndView("person",BindingResult.MODEL_KEY_PREFIX + "person",person);
    return mav;

Hope it would help others in the future too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文