gpt4 book ai didi

java - 不支持的媒体类型”,"exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content

转载 作者:行者123 更新时间:2023-11-29 07:29:37 25 4
gpt4 key购买 nike

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

RestController 方法发布

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

}

Ajax 调用

$( "#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'})
});
});

在你的 Controller 中:

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

}

关于java - 不支持的媒体类型”,"exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44805844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com