0

Hello StackOverFlow(s)

I'm running with this issue since more than 2 hours now It's simple

I'm trying to send a JSON object to a Spring Controller by using a $.ajax POST call

I'm using AngularJS but that point is okey

here's the code of both server and client and the spring configuration

Thank's in advance

JQuery :

    $scope.push = function() {
    $.ajax({
        type: "PUT",
        url:"rest/todo/greeting/",
        data : {id:"1",title:"ajax",description:"ajax"},
        dataType: "json",
        contentType : "application/json",
        success : function(data) {
            $log.info(data)
        }
    })
}

Spring Controller :

@Controller
@RequestMapping("/todo")
public class TodoController {
@RequestMapping(value = "/greeting", method = RequestMethod.PUT,consumes="application/json",produces="text/html")
public @ResponseBody String push(@RequestBody Todo todo) {
    System.out.println(todo.getTitle());
    return "test";
}

}

Spring Configuration :

<mvc:annotation-driven />
<context:component-scan base-package="org.lab.todo.controller" />
<bean id="defaultViews" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />

web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<!--
    Spring WEBMVC/REST Controllers
-->
<servlet>
    <servlet-name>todo-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>todo-dispatcher</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Ajax Call Update :

    $scope.push = function() {
    var jsonString = {id:"1",title:"ajax",description:"ajax"};
    var Todo = function(){}
    Todo.id = "id";
    Todo.title = "ajax";
    Todo.description = "ajax";
    $.post("rest/todo/greeting",JSON.stringify(Todo),function(response){console.log(response)},'json');
}

Fiddle Raw Request Header :

POST http://localhost:8080/todo-rest/rest/todo/greeting HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 0
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://localhost:8080
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko)     Chrome/24.0.1312.56 Safari/537.17
Referer: http://localhost:8080/todo-rest/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,fr;q=0.6
Accept-Charset: UTF-8,*;q=0.5

Fiddle Response Header

HTTP/1.1 415 Unsupported Media Type
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1048
Date: Thu, 31 Jan 2013 17:11:40 GMT
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Abderrazak BOUADMA
  • 1,526
  • 2
  • 16
  • 38

3 Answers3

5

Here's the answer

It's simply a matter of properly writing the JSON string

instead of

var jsonString = {id:"1",title:"ajax",description:"ajax"};

I wrote it like this

var jsonString = '{"id":"1","title":"ajax","description":"ajax"}';

what is still weird is that JSON.stringify(MyObject) seems not to work in my case !!

StormeHawke
  • 5,987
  • 5
  • 45
  • 73
Abderrazak BOUADMA
  • 1,526
  • 2
  • 16
  • 38
1

If you're getting a 400 status code, it can only be for the following reason (from W3):

10.4.1 400 Bad Request

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

With Spring, this either means that the ajax request is wrong (see my comment about the data JSON string) or it can't parse and translate the JSON String in the request body to your command object Todo. So Spring has a method it can call, your push() method, but it doesn't have the parameter your are looking for to pass to it, so it throws a 400 Bad Request.

415 Unsupported Media Type means that Spring cannot find a method to consume what you are sending. Your push() Spring method consumes="application/json" but your request isn't using that content-type.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Can you not see the body of the request with Fiddle? Also, you changed your request to POST, did you change your controller method? You also need to add a header for `Content-type: application/json` in your request. – Sotirios Delimanolis Jan 31 '13 at 17:21
  • nothing appears in the body of the request throw Fiddle and yes the controller has been updated. contentType overrides the header Content-type and it seems appearing within the request header -see update- thanks – Abderrazak BOUADMA Jan 31 '13 at 17:23
  • Yeah but you don't have contentType in your new `$.post()` function call. – Sotirios Delimanolis Jan 31 '13 at 17:25
  • indeed but it seems it adds it automatically at POST time. I used Fiddle Composer and put my json object withinthe request body and it's always responding 415 seems now it's spring side I'll investigate. Thanks – Abderrazak BOUADMA Jan 31 '13 at 17:32
0

Could you post your web.xml? Since you're not defining a suffix for your request and place a slash in the end, you're doing a request to the index.jsp of that directory.

Kurt Du Bois
  • 7,550
  • 4
  • 25
  • 33
  • 1
    2 things: Could you try to see through firebug to what url your ajax request is performed? Secondly: could you try removing your closing slash in your ajax-request? – Kurt Du Bois Jan 31 '13 at 16:28
  • the correct url is called, I tried after I removed the last slash and the response was the same. I updated the Post by adding a public SVN repo containing the project, use mvn tomcat:run to launch. – Abderrazak BOUADMA Jan 31 '13 at 16:43