简体   繁体   中英

Sending JSON to Spring MVC - Error 415

I'm trying to send JSON from a form to a spring mvc controller, but i'm always getting error 415.
I have already tried changing headers, types, stringify and etc. as said in other posts, without success.
Could someone help? I'm new in this and still trying to understand.

Main page function:

<script type="text/javascript">
function ConvertFormToJSON(form) {
    var array = jQuery(form).serializeArray();
    var json = {};

    jQuery.each(array, function() {
        json[this.name] = this.value || '';
    });

    return json;
}

jQuery(document).ready(function() {
    jQuery('#novoitem').submit(function() {

        var form = this;
        var json = ConvertFormToJSON(form);
        console.log(JSON.stringify(json));
        jQuery.ajax({
            dataType : "json",
            contentType : "application/json",
            type : "POST",
            url : "inventario/",
            data : JSON.stringify(json),
            contentType : "application/json",
            success : function(data) {
                alert(data);
            }
        });

        return false;
    });
});

Form:

<form id="novoitem" method="post" enctype='application/json'>
    <label for="usuario">Usuario:</label> 
    <input id="usuario" name="usuario" type="text">
    <label for="tipo">Tipo:</label>
    <input id="tipo" name="tipo" type="text">
    <label for="nomeItem">Item:</label>
    <input id="nomeItem" name="nomeItem" type="text">
    <label for="quantidade">Quantidade:</label>
    <input id="quantidade" name="quantidade"type="text">
    <label for="vencimento">Vencimento:</label>
    <input id="vencimento" name="vencimento" type="text">
    <input type="submit" value="Incluir">
</form>

Controller:

@RequestMapping(value = "/inventario/", method = RequestMethod.POST)
public ResponseEntity<Void> insereItem(@RequestBody Item item){...} 

Console.log Stringify:

{"usuario":"a","tipo":"b","nomeItem":"c","quantidade":"d","vencimento":"e"}

Error:

POST http://localhost:8888/inventario/ 415 (Unsupported Media Type)

you have to add a MediaType in your request mapping in your controller like this.

@RequestMapping(value = "/inventario/",consumes = MediaType.APPLICATION_JSON, 
                method = RequestMethod.POST)
public ResponseEntity<Void> insereItem(@RequestBody Item item){...} 

eighter you have remove the datatype while sending ajax request ie,

  dataType : "json"

or you have to produce the application/json response as below

   @RequestMapping(value = "/inventario/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)

it will gives the response as JSON object which is matched with ajax response dataType key. you can use any one of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM