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

Bob*_*obo 35 content-type spring-mvc

代码段:

@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");
}
Run Code Online (Sandbox Code Playgroud)

收到请求后,我得到的是Http状态代码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");
}
Run Code Online (Sandbox Code Playgroud)

我将不允许405方法.有趣的是在允许的响应标题中,它将GET和POST列为允许的方法.

我有一个做JOSN映射的类:

@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;
}

}
Run Code Online (Sandbox Code Playgroud)

复制了Spring的例子.适用于JSON内容类型.

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

axt*_*avt 55

不幸的是FormHttpMessageConverter(@RequestBody当内容类型是用于注释参数时application/x-www-form-urlencoded)不能绑定目标类(如同@ModelAttribute).

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

@RequestMapping(method = RequestMethod.POST)
public ModelAndView create(@ModelAttribute UserAccountBean account) { ... }
Run Code Online (Sandbox Code Playgroud)

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

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

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


use*_*828 22

我的HTTP响应代码为415

当我将内容类型添加到请求标头时,我的问题得到解决

例如

"内容类型:application/json"