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

查看:21
本文介绍了带有请求内容类型表单的 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 Method not allowed.有趣的是在响应的允许标头中,它将 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) { ... }

否则我猜你可以创建一个单独的方法表单处理具有适当的headers 属性的表单数据:

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) { ... }

另一种可能的选择是通过组合 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天全站免登陆