JSON plus spring mvc 3.2 error 415 (unsupported media type)

What did I do wrong? I am trying to use Spring mvc and JSON. When I try to debug my code, I see that javascript is working but not working with the controller. Error 415 appears on the browser. Unsupported media type.

Script:

$(document).ready(function() {
  $('#newSmartphoneForm').submit(function(event) {

      var producer = $('#producer').val();
      var model = $('#model').val();
      var price = $('#price').val();
      var json = { "producer" : producer, "model" : model, "price": price};

    $.ajax({
        url: $("#newSmartphoneForm").attr( "action"),
        data: JSON.stringify(json),
        type: "POST",

        beforeSend: function(xhr) {
            xhr.setRequestHeader("Accept", "application/json");
            xhr.setRequestHeader("Content-Type", "application/json");
        },
        success: function(smartphone) {
            var respContent = "";

            respContent += "<span class='success'>Smartphone was    created: [";
            respContent += smartphone.producer + " : ";
            respContent += smartphone.model + " : " ;
            respContent += smartphone.price + "]</span>";

            $("#sPhoneFromResponse").html(respContent);         
        }
    });

    event.preventDefault();
  });

});  

Controllers

   @RequestMapping(value="/create", method=RequestMethod.POST, 
        produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Smartphone createSmartphone(@RequestBody Smartphone smartphone) {
    return smartphoneService.create(smartphone);
}
+4
source share
1 answer

This can happen because you do not have Jackson in your classpath at runtime.

, - JSON. JSON Java , . <mvc:annotation-driven /> Spring XML ( Java Config enabled), JSON . , .

+5

Source: https://habr.com/ru/post/1523637/


All Articles