我在@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);
}
问题如果我尝试执行方法,我会收到以下消息:
出现意外错误(type = Not Found,status = 404)。
如果我删除:
@requestpost参数的headers = {“Accept = application / x-www-form-urlencoded”,“Content-Type = 应用程序/ x WWW的形式进行了urlencoded”}
我有以下错误:
出现意外错误(type = Unsupported Media Type, 状态= 415)。内容类型 'application / x-www-form-urlencoded; charset = UTF-8'不支持
N.B:我已经访问了这个post,但它没有解决我的问题
提前感谢您的帮助。
答案 0 :(得分:-1)
尝试consumes
@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)