Spring 3.0.5 - 将 @ModelAttribute 添加到处理程序方法签名会导致 JsonMappingException

发布于 2024-11-02 23:04:46 字数 9589 浏览 0 评论 0原文

我不确定这是否是我的配置错误,是对通过 @ModelAttribute 和自动 JSON 内容转换可以完成的工作的误解,还是 Spring 或 Jackson 中的错误。当然,如果结果是后者,我会向相应的人员提出问题。

我在将 @ModelAttribute 添加到控制器的处理程序方法时遇到了问题。该方法的目的是公开一个从表单或先前提交填充的 bean,但我可以重现该问题,而无需实际将数据提交到该 bean 中。

我正在使用 Spring mvc-showcase 示例。它当前使用 Spring 3.1,但我第一次遇到并且能够在我的 3.0.5 设置上重现这个问题。 mvc-showcase 示例使用非常标准的 servlet-context.xml:

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    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">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven conversion-service="conversionService">
        <argument-resolvers>
            <beans:bean class="org.springframework.samples.mvc.data.custom.CustomArgumentResolver"/>
        </argument-resolvers>
    </annotation-driven>

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <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>

    <!-- Imports user-defined @Controller beans that process client requests -->
    <beans:import resource="controllers.xml" />

    <!-- Only needed because we install custom converters to support the examples in the org.springframewok.samples.mvc.convert package -->
    <beans:bean id="conversionService" class="org.springframework.samples.mvc.convert.CustomConversionServiceFactoryBean" />

    <!-- Only needed because we require fileupload in the org.springframework.samples.mvc.fileupload package -->
    <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

</beans:beans>

文件中引用的 controllers.xml 只是为根设置相关的组件扫描和视图控制器小路。相关片段如下。

controllers.xml

<!-- Maps '/' requests to the 'home' view -->
<mvc:view-controller path="/" view-name="home"/>

<context:component-scan base-package="org.springframework.samples.mvc" />

我试图提供的测试 bean 是一个非常简单的 POJO。

TestBean.java

package org.springframework.samples.mvc.test;

public class TestBean {
    private String testField = "[email protected]";

    public String getTestField() {
        return testField;
    }

    public void setTestField(String testField) {
        this.testField = testField;
    }

}

最后是控制器,它也很简单。

TestController.java

package org.springframework.samples.mvc.test;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("test/*")
public class TestController {

    @ModelAttribute("testBean")
    public TestBean getTestBean() {
        return new TestBean();
    }

    @RequestMapping(value = "beanOnly", method = RequestMethod.POST)
    public @ResponseBody
    TestBean testBean(@ModelAttribute("testBean") TestBean bean) {
        return bean;
    }

    @RequestMapping(value = "withoutModel", method = RequestMethod.POST)
    public @ResponseBody
    Model testWithoutModel(Model model) {
        model.addAttribute("result", "success");
        return model;
    }

    @RequestMapping(value = "withModel", method = RequestMethod.POST)
    public @ResponseBody
    Model testWithModel(Model model, @ModelAttribute("testBean") TestBean bean) {
        bean.setTestField("This is the new value of testField");
        model.addAttribute("result", "success");
        return model;
    }

}

如果我通过映射路径 /mvc-showcase/test/beanOnly 调用控制器,我将按照预期获得 Bean 的 JSON 表示形式。调用 withoutModel 处理程序会传递与调用关联的 Spring Model 对象的 JSON 表示形式。它在返回值中包含初始声明中的隐式 @ModelAttribute,但该 bean 对于该方法不可用。例如,如果我希望处理表单提交的结果并返回 JSON 响应消息,那么我需要该属性。

最后一个方法添加了@ModelAttribute,这就是问题出现的地方。调用 /mvc-showcase/test/withModel 会导致异常。

在我的 3.0.5 安装中,由于缺少 FormattingConversionService 的序列化程序而导致出现 JsonMappingException。在 3.1.0 示例中,异常是由于缺少 DefaultConversionService 的序列化程序而导致的。我将在这里包括 3.1 例外;尽管路径略有不同,但似乎具有相同的根本原因。

3.1 org.codehaus.jackson.map.JsonMappingException

org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.springframework.format.support.DefaultFormattingConversionService and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.validation.support.BindingAwareModelMap["org.springframework.validation.BindingResult.testBean"]->org.springframework.validation.BeanPropertyBindingResult["propertyAccessor"]->org.springframework.beans.BeanWrapperImpl["conversionService"])
    at org.codehaus.jackson.map.ser.StdSerializerProvider$1.failForEmpty(StdSerializerProvider.java:89)
    at org.codehaus.jackson.map.ser.StdSerializerProvider$1.serialize(StdSerializerProvider.java:62)
    at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:272)
    at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:175)
    at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:147)
    at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:272)
    at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:175)
    at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:147)
    at org.codehaus.jackson.map.ser.MapSerializer.serializeFields(MapSerializer.java:207)
    at org.codehaus.jackson.map.ser.MapSerializer.serialize(MapSerializer.java:140)
    at org.codehaus.jackson.map.ser.MapSerializer.serialize(MapSerializer.java:22)
    at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:315)
    at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:242)
    at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1030)
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.writeInternal(MappingJacksonHttpMessageConverter.java:153)
    at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:181)
    at org.springframework.web.servlet.mvc.method.annotation.support.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:121)
    at org.springframework.web.servlet.mvc.method.annotation.support.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:101)
    at org.springframework.web.servlet.mvc.method.annotation.support.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:81)
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:64)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMethodAdapter.invokeHandlerMethod(RequestMappingHandlerMethodAdapter.java:505)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMethodAdapter.handleInternal(RequestMappingHandlerMethodAdapter.java:468)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at 
...

那么,我是否缺少一些配置,这些配置应该允许 Jackson 转换器正确处理从方法签名中带有 @ModelAttribute 的处理程序派生的响应?如果不是,有什么想法这更有可能是 Spring bug 还是 Jackson bug?在这一点上,我倾向于春天。

I'm not sure whether this is a misconfiguration on my part, a misunderstanding of what can be accomplished via @ModelAttribute and automatic JSON content conversion, or a bug in either Spring or Jackson. If it turns out to be the latter, of course, I'll file an issue with the appropriate folks.

I've encountered a problem with adding a @ModelAttribute to a controller's handler method. The intent of the method is to expose a bean that's been populated from a form or previous submission, but I can reproduce the issue without actually submitting data into the bean.

I'm using the Spring mvc-showcase sample. It's currently using Spring 3.1, but I first encountered, and am able to reproduce, this issue on my 3.0.5 setup. The mvc-showcase sample uses a pretty standard servlet-context.xml:

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    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">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven conversion-service="conversionService">
        <argument-resolvers>
            <beans:bean class="org.springframework.samples.mvc.data.custom.CustomArgumentResolver"/>
        </argument-resolvers>
    </annotation-driven>

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <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>

    <!-- Imports user-defined @Controller beans that process client requests -->
    <beans:import resource="controllers.xml" />

    <!-- Only needed because we install custom converters to support the examples in the org.springframewok.samples.mvc.convert package -->
    <beans:bean id="conversionService" class="org.springframework.samples.mvc.convert.CustomConversionServiceFactoryBean" />

    <!-- Only needed because we require fileupload in the org.springframework.samples.mvc.fileupload package -->
    <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

</beans:beans>

The controllers.xml referenced in the file simply sets up the relevant component-scan and view-controller for the root path. The relevant snippet is below.

controllers.xml

<!-- Maps '/' requests to the 'home' view -->
<mvc:view-controller path="/" view-name="home"/>

<context:component-scan base-package="org.springframework.samples.mvc" />

The test bean which I am attempting to deliver is a dead-simple POJO.

TestBean.java

package org.springframework.samples.mvc.test;

public class TestBean {
    private String testField = "[email protected]";

    public String getTestField() {
        return testField;
    }

    public void setTestField(String testField) {
        this.testField = testField;
    }

}

And finally, the controller, which is also simple.

TestController.java

package org.springframework.samples.mvc.test;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("test/*")
public class TestController {

    @ModelAttribute("testBean")
    public TestBean getTestBean() {
        return new TestBean();
    }

    @RequestMapping(value = "beanOnly", method = RequestMethod.POST)
    public @ResponseBody
    TestBean testBean(@ModelAttribute("testBean") TestBean bean) {
        return bean;
    }

    @RequestMapping(value = "withoutModel", method = RequestMethod.POST)
    public @ResponseBody
    Model testWithoutModel(Model model) {
        model.addAttribute("result", "success");
        return model;
    }

    @RequestMapping(value = "withModel", method = RequestMethod.POST)
    public @ResponseBody
    Model testWithModel(Model model, @ModelAttribute("testBean") TestBean bean) {
        bean.setTestField("This is the new value of testField");
        model.addAttribute("result", "success");
        return model;
    }

}

If I call the controller via the mapped path /mvc-showcase/test/beanOnly, I get a JSON representation of the bean, as expected. Calling the withoutModel handler delivers a JSON representation of the Spring Model object associated with the call. It includes the implicit @ModelAttribute from the initial declaration in the return value, but the bean is unavailable to the method. If I wish to process the results of a form submission, for example, and return a JSON response message, then I need that attribute.

The last method adds the @ModelAttribute, and this is where the trouble comes up. Calling /mvc-showcase/test/withModel causes an exception.

In my 3.0.5 installation, I get a JsonMappingException caused by a lack of serializer for FormattingConversionService. In the 3.1.0 sample, the exception is caused by lack of serializer for DefaultConversionService. I'll include the 3.1 exception here; it seems to have the same root cause, even if the path is a bit different.

3.1 org.codehaus.jackson.map.JsonMappingException

org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.springframework.format.support.DefaultFormattingConversionService and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.validation.support.BindingAwareModelMap["org.springframework.validation.BindingResult.testBean"]->org.springframework.validation.BeanPropertyBindingResult["propertyAccessor"]->org.springframework.beans.BeanWrapperImpl["conversionService"])
    at org.codehaus.jackson.map.ser.StdSerializerProvider$1.failForEmpty(StdSerializerProvider.java:89)
    at org.codehaus.jackson.map.ser.StdSerializerProvider$1.serialize(StdSerializerProvider.java:62)
    at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:272)
    at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:175)
    at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:147)
    at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:272)
    at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:175)
    at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:147)
    at org.codehaus.jackson.map.ser.MapSerializer.serializeFields(MapSerializer.java:207)
    at org.codehaus.jackson.map.ser.MapSerializer.serialize(MapSerializer.java:140)
    at org.codehaus.jackson.map.ser.MapSerializer.serialize(MapSerializer.java:22)
    at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:315)
    at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:242)
    at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1030)
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.writeInternal(MappingJacksonHttpMessageConverter.java:153)
    at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:181)
    at org.springframework.web.servlet.mvc.method.annotation.support.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:121)
    at org.springframework.web.servlet.mvc.method.annotation.support.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:101)
    at org.springframework.web.servlet.mvc.method.annotation.support.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:81)
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:64)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMethodAdapter.invokeHandlerMethod(RequestMappingHandlerMethodAdapter.java:505)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMethodAdapter.handleInternal(RequestMappingHandlerMethodAdapter.java:468)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at 
...

So, is there some configuration I am missing that should allow the Jackson converter to properly handle a response derived from a handler with @ModelAttribute in the method signature? If not, any thoughts as to whether this is more likely a Spring bug or a Jackson bug? I'm leaning toward Spring, at this point.

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

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

发布评论

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

评论(1

穿越时光隧道 2024-11-09 23:04:46

它看起来像一个 Spring 配置问题,当序列化为 JSON 时,DefaultFormattingConversionService 为空,如果 bean 为空,Jackson(默认情况下)将抛出异常,请参阅 FAIL_ON_EMPTY_BEANS 中的 < a href="http://wiki.fasterxml.com/JacksonFeaturesSerialization" rel="nofollow">功能文档。但我不清楚为什么豆子是空的。

如果您将 FAIL_ON_EMPTY_BEANS 设置为 false,它应该可以工作,但仍然不能真正解释为什么会发生这种情况。

DefaultFormattingConversionService 是 3.1 的新增内容 - 它扩展了 FormattingConversionService,解释了 3.0.5 和 3.1 之间的不同异常。

我不认为这是杰克逊的问题,尽管杰克逊的新版本(1.8. 0)仅在 3 天前发布,因此您也可以尝试一下。

我将尝试在本地重现此内容。

It looks like a Spring config problem, when serializing to JSON the DefaultFormattingConversionService is empty and Jackson (by default) will throw an exception if a bean is empty see FAIL_ON_EMPTY_BEANS in the features documentation. But I am not clear why the bean is empty.

It should work if you set FAIL_ON_EMPTY_BEANS to false, but still doesn't really explain why it is happening in the first place.

DefaultFormattingConversionService is new to 3.1 - it extends the FormattingConversionService which explains the different exceptions between 3.0.5 and 3.1.

I do not think it is a Jackson problem, although a new version of Jackson (1.8.0) was released only 3 days ago so you could try that also.

I will try to reproduce this locally.

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