Spring @RequestBody不起作用

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

我对spring的@requestbody注释有问题。实际上我无法恢复和转换我的表单数据。我有这个例外:

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

我正在使用1.5.8版本的弹簧启动

这是我的春季代码:

    @RequestMapping(value = "/insert",method = RequestMethod.POST,consumes = MediaType.APPLICATION_FORM_URLENCODED)
public void createType(@RequestBody Type type) {
     typeService.createType(type);
}

我试过没有@RequestBody,它也不起作用。

这是我使用vuejs和axios的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" />
</head>
<body>
    <div id="app">
    <h1>TEST FORM</h1>
     <form action="#" method="post">
        <p>Type description: <input type="text" v-model="description"/></p>
        <li v-for="some in someData"> {{ some }} </li>
        <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:{
                description:'',
                someData:[]
            },
            methods:{
                addType(){
                    this.$http.post('/types/insert',{description:this.description}).then(response => {
                        this.someData:response.data;
                    });
                }
            }
        });
      </script>
</body>
</html>

先感谢您...

java spring-boot axios
1个回答
0
投票

您使用类型application/x-www-form-urlencoded;charset=UTF-8进行客户端调用,只应使用application/x-www-form-urlencoded

也许你可以在你的HTML中做到这一点:

const config = { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } };
this.$http.post(
   '/types/insert',
   {description:this.description}).then(response => {
                    this.someData:response.data;
                },
   config);

也许你也可以试试

@RequestMapping(... @consumes = "application/x-www-form-urlencoded;charset=UTF-8")

但这看起来是一个糟糕的解决方案,即使它有效。

© www.soinside.com 2019 - 2024. All rights reserved.