2017-12-21 20 views
-1

私はspringの@requestbodyアノテーションに問題があります。実際には、私のフォームのデータを回復して変換することはできません。 私はこの例外を持っている: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せずに試してみました仕事も。

そして、ここに私のhtmlがvuejsとaxiosを使用している:

<!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> 

は、事前にあなたに感謝...

+0

[application/x-www-form-urlencodedとcharset = "utf-8"の可能な複製?](https://stackoverflow.com/questions/16819502/application-x-www-form-urlencoded-and) -charset-utf-8) – tkruse

+0

https://stackoverflow.com/questions/34782025/http-post-request-with-content-type-application-x-www-form-urlencoded-not-workin/38252762#も参照してください。 38252762、https://stackoverflow.com/questions/33796218/content-type-application-x-www-form-urlencodedcharset-utf-8-not-supported-for – tkruse

+0

私はすでにこの投稿にアクセスしましたが、私の問題は解決しません問題。 – kasko

答えて

0

あなたのクライアントは、application/x-www-form-urlencodedを使用する必要があり、タイプapplication/x-www-form-urlencoded;charset=UTF-8を呼び出します。

たぶん、あなたはあなたの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") 

を試すことができますしかし、それはそれが動作しても、悪い解決策のように見えます。

+0

こんにちは、私はあなたの反応を理解していません。どうすればいいですか? – kasko

+0

私はvuejsの専門家ではありませんが、使用するコンテンツタイプを変更する必要があります。多分ここを見てください:https://github.com/axios/axios/issues/362 – tkruse

+0

ありがとうございますが、私はすでにこのディスカッションに訪れました。 – kasko

関連する問題