繁体   English   中英

不支持的媒体类型,“例外”:“ org.springframework.web.HttpMediaTypeNotSupportedException”,“消息”:“内容”

[英]Unsupported Media Type“,”exception“:”org.springframework.web.HttpMediaTypeNotSupportedException“,”message“:”Content

我有一个带有以下邮政编码的RestController,并尝试在我的AJAX中使用它在数据库表中添加数据。 我有以下错误。

RestController方法发布

@RequestMapping(method = RequestMethod.POST)
    public void create(@RequestBody MessageChat chatmessage) {
        messageService.save(chatmessage);

    }

阿贾克斯电话

$( "#vut" ).on( "click", function(e){
     e.preventDefault();
        e.stopPropagation();
        $.ajax({
               url: '/messagechat',
               method: 'POST',ContentType:'application/json',dataType: 'JSON',
               data: {
                      message: 'a'
                  }
                })

    });

错误

{"timestamp":1498660502132,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported","path":"/messagechat"}

有人可以帮忙吗?

它应该是

$( "#vut" ).on( "click", function(e){
    e.preventDefault();
    e.stopPropagation();
    $.ajax({
           url: '/messagechat',
           method: 'POST',
           dataType: 'json',
           data: JSON.stringify({message: 'a'})
    });
});

在您的控制器中:

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public void create(@RequestBody MessageChat chatmessage) {
    messageService.save(chatmessage);

}

根据jQuery函数ajax()文档,应将属性名称从ContentType更改为contentType JavaScript标识符不区分大小写。

尝试使用“ c ontentType”代替“ C ontentType”

尝试

@RequestMapping(method = RequestMethod.POST, headers = "Accept=*/*)

另外,您也可以将其设置为headers =“ Accept = application / json”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM