gpt4 book ai didi

java - @RequestMapping(headers)、@RequestBody 不起作用

转载 作者:行者123 更新时间:2023-12-01 06:04:23 25 4
gpt4 key购买 nike

我在 @RequestMapping 注释中配置 header 属性时遇到问题。这是我的代码:HTML 页面:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" th:src="@{/css/url_rearch_params.js}"/>
</head>
<body>
<div id="app">
<h1>TEST FORM</h1>
<form action="" method="post">
<p>Type description: <input type="text" v-model="type.description"/></p>
<p><button v-on:click="addType()"> Send </button><input type="reset" value="Reset" /></p>
</form>
</div>


<script src="http://cdn.jsdelivr.net/vue/1.0.10/vue.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
Vue.prototype.$http = axios;
new Vue({
el:'#app',
data:{
type:{description:''}
},
methods:{
addType(){
const config = { headers: { 'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/x-www-form-urlencoded'
}
};
let newType = {description:this.type.description};
console.log(newType);
this.$http.post('/types/insert',newType,config).then(response => {
console.log(response);
});
}
}
});
</script>
</body>
</html>

还有我的java代码:

@RequestMapping(value = "/insert",method = RequestMethod.POST, headers = {"Accept=application/x-www-form-urlencoded","Content-Type = application/x-www-form-urlencoded"},consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public @ResponseBody void createType(@RequestBody Type type) {
System.out.println(type);
typeService.createType(type);
}

如果我尝试执行该方法,则会出现以下问题:

There was an unexpected error (type=Not Found, status=404).

如果我删除:

headers = {“Accept=application/x-www-form-urlencoded”,“Content-Type = application/x-www-form-urlencoded”}

@requestpost 参数我有以下错误:

There was an unexpected error (type=Unsupported Media Type, status=415). Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported

注意:我已经访问过这个 post但这并不能解决我的问题

预先感谢您的帮助。

最佳答案

尝试使用consumes代替 header

@RequestMapping(value = "/insert",method = RequestMethod.POST, consumes="application/x-www-form-urlencoded","Content-Type = application/x-www-form-urlencoded"},consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)

关于java - @RequestMapping(headers)、@RequestBody 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47955862/

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