带有请求内容类型表单的 Http Post 在 Spring MVC 3 中不起作用

2023-12-27

代码片段:

@RequestMapping(method = RequestMethod.POST)//,  headers = "content-type=application/x-www-form-urlencoded")
public ModelAndView create(@RequestBody UserAccountBean account) {
    try{
        accounts.put(account.assignId(), account);
    }catch(RuntimeException ex)
    {
        return new ModelAndView("account/registerError");
    }
    return new ModelAndView("account/userVerification");
}

收到请求后,我得到的是 Http Status code 415: 服务器拒绝此请求,因为请求实体的格式不受所请求方法 () 的请求资源支持。

如果我将代码更改为:

代码片段:

@RequestMapping(method = RequestMethod.POST,headers = "content-type=application/x-www-form-urlencoded")
public ModelAndView create(@RequestBody UserAccountBean account) {
    try{
        accounts.put(account.assignId(), account);
    }catch(RuntimeException ex)
    {
        return new ModelAndView("account/registerError");
    }
    return new ModelAndView("account/userVerification");
}

我会收到 405 Method not allowed。有趣的是在响应的允许标头中,它将 GET 和 POST 列为允许的方法。

我确实有一个执行 JSON 映射的类:

@Component
public class JacksonConversionServiceConfigurer implements BeanPostProcessor {

private final ConversionService conversionService;

@Autowired
public JacksonConversionServiceConfigurer(ConversionService conversionService) {
    this.conversionService = conversionService;
}

public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    return bean;
}

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (bean instanceof AnnotationMethodHandlerAdapter) {
        AnnotationMethodHandlerAdapter adapter = (AnnotationMethodHandlerAdapter) bean;
        HttpMessageConverter<?>[] converters = adapter.getMessageConverters();
        for (HttpMessageConverter<?> converter : converters) {
            if (converter instanceof MappingJacksonHttpMessageConverter) {
                MappingJacksonHttpMessageConverter jsonConverter = (MappingJacksonHttpMessageConverter) converter;
                jsonConverter.setObjectMapper(new ConversionServiceAwareObjectMapper(this.conversionService));
            }               
        }
    }
    return bean;
}

}

从 Spring 示例复制。与 JSON 内容类型配合得很好。

一个更普遍的问题是如何使 spring mvc 请求处理程序处理不同的请求内容类型。 任何建议将不胜感激。


很遗憾FormHttpMessageConverter(用于@RequestBody-内容类型为时带注释的参数application/x-www-form-urlencoded)无法绑定目标类(如@ModelAttribute can).

因此你需要@ModelAttribute代替@RequestBody。如果您不需要将不同的内容类型传递给该方法,您可以简单地替换注释:

@RequestMapping(method = RequestMethod.POST)
public ModelAndView create(@ModelAttribute UserAccountBean account) { ... }

否则我想你可以创建一个单独的方法来处理表单数据并使用适当的方法headers属性:

@RequestMapping(method = RequestMethod.POST, 
    headers = "content-type=application/x-www-form-urlencoded") 
public ModelAndView createFromForm(@ModelAttribute UserAccountBean account) { ... }

EDIT:另一种可能的选择是实现您自己的HttpMessageConverter通过结合FormHttpMessageConverter(将输入消息转换为参数映射)和WebDataBinder(将参数映射转换为目标对象)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带有请求内容类型表单的 Http Post 在 Spring MVC 3 中不起作用 的相关文章

随机推荐