2017-12-21 2 views
-1

ich ein Problem mit der @requestbody Annotation von spring.In der Tat kann ich die Daten meiner Form gewinnen und konvertieren. Ich habe diese Ausnahme:Frühling @RequestBody funktioniert nicht

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

Ich bin mit Version 1.5.8 des Frühlings Boot

Hier ist meine Feder Code:

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

ich ohne @RequestBody versucht, es nicht arbeite auch.

Und hier ist mein html vuejs und axios mit:

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

Vielen Dank im Voraus ...

+0

Mögliches Duplikat von [application/x-www-form-urlencoded und charset = "utf-8"?] (Https://stackoverflow.com/questions/16819502/application-x-www-form-urlencoded-and -charset-utf-8) – tkruse

+0

siehe auch 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

Ich habe diesen Beitrag bereits besucht, aber es löst nicht meine Problem. – kasko

Antwort

0

Sie Client ruft mit Typ application/x-www-form-urlencoded;charset=UTF-8 sollte nur application/x-www-form-urlencoded verwenden.

Vielleicht können Sie dies in Ihrem html tun:

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); 

Vielleicht auch Sie

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

versuchen können, aber das sieht aus wie eine schlechte Lösung, auch wenn es funktioniert.

+0

Hallo, ich verstehe deine Antwort nicht. Wie kann ich das tun? – kasko

+0

Ich bin kein Vuejs-Experte, aber irgendwo müssen Sie den verwendeten Inhaltstyp ändern. Vielleicht sehen Sie hier: https://github.com/axios/axios/issues/362 – tkruse

+0

Vielen Dank, aber ich habe diese Diskussion bereits besucht. – kasko

Verwandte Themen