带有请求内容类型形式的Http Post在Spring MVC 3中不起作用 [英] Http Post with request content type form not working in Spring MVC 3

查看:98
本文介绍了带有请求内容类型形式的Http Post在Spring MVC 3中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码段:

@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状态码415:
服务器拒绝了此请求,因为请求实体的格式不受请求方法()的请求资源支持。

After receiving request, What I got is Http Status code 415: The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().

如果我将代码更改为:

If I change the code to this:

代码段:

@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方法不允许。有趣的是,在响应的allow标头中,它列出了GET和POST作为允许的方法。

I will get 405 Method not allowed. Funny thing is in the allow header of response, it lists GET and POST as allowed methods.

我确实有一个可以进行JOSN映射的类:

I do have a class that does JOSN mapping:

@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内容类型配合使用时效果很好。

Copied from Spring examples. works great with JSON content-type.

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

A more general question is how to make spring mvc request handlers work with different request content-types. Any advice would be greatly appreciated.

推荐答案

不幸的是 FormHttpMessageConverter (用于 @RequestBody 标记的内容类型为 application / x-www-form-urlencoded 的参数不能绑定目标类(因为 @ModelAttribute 可以)。

Unfortunately FormHttpMessageConverter (which is used for @RequestBody-annotated parameters when content type is application/x-www-form-urlencoded) cannot bind target classes (as @ModelAttribute can).

因此,您需要 @ModelAttribute 而不是 @RequestBody 。如果您不需要将不同的内容类型传递给该方法,则只需替换批注:

Therefore you need @ModelAttribute instead of @RequestBody. If you don't need to pass different content types to that method you can simply replace the annotation:

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

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

Otherwise I guess you can create a separate method form processing form data with the appropriate headers attribute:

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

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

Another possible option is to implement your own HttpMessageConverter by combining FormHttpMessageConverter (to convert input message to the map of parameters) and WebDataBinder (to convert map of parameters to the target object).

这篇关于带有请求内容类型形式的Http Post在Spring MVC 3中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆