• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Spring mvc request mapping for POST JSON request not working.

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
In our spring web application we are getting AJAX JSON request and I have to do the request mapping in controller.
Following is the structure of my request form data sent to the server.


Following is the request mapping I have done in the controller.






But I am getting the following error in the browser

415 Unsupported Media Type

Please let me know what is wrong with my request mapping. Thanks.

Regards,
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a side note you should use the 'produces' and 'consumes' attributes of RequestMapping to define the Content-Type and Accept rather than the headers attribute. This was added in newer versions of Spring.

The likely culprit is one of 2 things:

-Either you do not have the Jackson type converter registered (should happen if you have used the annotation-config tag in the MVC namespace or the @EnableWebMvc annotation in your @Configuration file) IF the Jackson library is on your classpath.
-You have not set the Content-Type header on the incoming request to "application/json"

The easiest way to do some test posts is use the RestClient Firefox extension. Using this plugin you can post in some JSON and set the Content-Type header.
https://addons.mozilla.org/en-us/firefox/addon/restclient/
 
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OP,

Following Bill's advice. This is what I did to solve my json issue using Spring 3

1.a. Add to applicationContext-mvc.xml



Then implicitly RequestMappingHandlerMapping will be registered/called/used.
Then MappingJacksonHttpMessageConverter will be called as it is registered.

3. Controller


4. Fun to debug:

- The best is use a debugger and set breaking point on below classes and others
DispatcherServlet
RequestMappingHandlerMapping
MappingJacksonHttpMessageConverter

-have TCP/IP to see the request sent from the client to server: http headers + body


-log4j.xml to watch log:
reply
    Bookmark Topic Watch Topic
  • New Topic