@RequestMapping(标题),@ RequestBody无效

问题描述 投票:1回答:1

我在@RequestMapping注释中配置头属性时遇到问题。这是我的代码: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)。

如果我删除:

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

@requestpost参数我有以下错误:

出现意外错误(type = Unsupported Media Type,status = 415)。内容类型'application / x-www-form-urlencoded; charset = UTF-8'不受支持

N.B:我已经访问过这个post,但它并没有解决我的问题

预先感谢您的帮助。

java spring-boot axios
1个回答
-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)
© www.soinside.com 2019 - 2024. All rights reserved.