2

I have Spring REST application that can marshall an object to JSON when a GET request occurs. However what should I do for POST methods. I send a JSON object but it can't unmarshall it into a Java object. Here is a method of my controller:

@RequestMapping(value = "/user", method = RequestMethod.POST)
public void createUser(HttpServletResponse response, @RequestBody User user) { 
   ...
    response.setStatus(HttpServletResponse.SC_OK);
}

I send my JSON like that:

        var userName = $('#userName').val();
        var password = $('#password').val();
        var mail = $('#mail').val();
        var admin = $("#admin").is(':checked');
        var user = {userName: userName, password: password, mail: mail, admin:admin};
        $.ajax({
            async : false,
            type: 'POST',
            contentType: 'application/json',
            url: '/sfd/user',
            data: user,
            dataType: 'json',
            success: function(data) {
               ...
            },
            error: function(data) {
                ...
            }
        });

PS: Here: http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/ it says:

If there are validation errors, a HTTP 400 is returned with the error messages, otherwise a HTTP 200 is returned.

I have 400 Bad Request Error. Maybe the problem is related to that?

PS2: Should I send User object after I set all its elements? I mean User object has some other attributes, i.e. address but I don't set and send it from client.

PS3: When I debug AbstractHandlerExceptionResolver's resolveException method I see that error. It says 'u' is undefined character(I tested it that 'u' is the first key of JSON => userName's first character).

kamaci
  • 72,915
  • 69
  • 228
  • 366

2 Answers2

2

You should have the following setting in your XML:

<context:annotation-config/> 

and then to make sure that jackson jar is in your classpath:

in my projects I prefer to set it as Spring Bean:

it will deserialize your json data to object.

You can read it more about it here:

http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/

danny.lesnik
  • 18,479
  • 29
  • 135
  • 200
  • I still have 400 Bad Request Error. My question is related to here: http://stackoverflow.com/questions/7481055/sending-json-data-to-server-error – kamaci Sep 20 '11 at 10:57
0

In the @RequestMapping set headers to accept application/json, and then try configuring this in your application context

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" p:order="1">
<property name="mediaTypes">
    <map>
        <entry key="json" value="application/json" />
    </map>
</property>

<property name="defaultViews">
    <list>
        <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
    </list>
</property>
</bean>
geez
  • 106
  • 3
  • It gives error at p:order="1" because it wants prefix for beans? – kamaci Sep 21 '11 at 06:02
  • Place xmlns:p="http://www.springframework.org/schema/p" in the namespace declaration of the beans element of the spring context – geez Sep 21 '11 at 21:45