title: JSON request with JQuery/Ajax with Spring
body: <p>I'm very new to JSON/Spring MVC and am trying to get a simple example of an AJAX call to a Spring MVC controller - but I keep returning an error of 400 - Bad Request.   </p>
  
  <p>After scouring the Internet, I've found that this is usually caused by not setting the appropriate content type - but [I believe] I have done that.</p>
  
  <p>Here is my AJAX call:</p>
  
  <pre><code>    //validate the object
      var urlString = "/ajax/add/";
  
      $.ajax({
          type:"POST",
          url: urlString,
          contentType: "application/json; charset=utf-8",
          dataType: "json", 
          data: {value1: 'apples', value2 : 'oranges'},
          success: function(result){
              alert("success");
          },
          error: function(jqXHR, textStatus, errorThrown){
              alert("error:" + textStatus + " exception:" + errorThrown);
          }
  
      }) ;
  </code></pre>
  
  <p>And my controller:</p>
  
  <pre><code>@Controller
  itpublic class AddController {
  
  private static Logger m_log = null;
  
  @RequestMapping(value = "/ajax/add")
  public String handle(@RequestBody String json){
  
      DummyClass dummyRequest;
      try {
  
          ObjectMapper mapper = new ObjectMapper();
          dummyRequest = mapper.readValue(json, DummyClass.class);
          m_log.info("Value1: " + dummyRequest.getValue1());
          m_log.info("Value2: " + dummyRequest.getValue2());
      }  catch (Exception e) {
  
      }
      finally{
  
  
      }
      return "called";
  }
  </code></pre>
  
  <p>Can anyone help me out?</p>
  
  <p><em>edit</em></p>
  
  <p>Here is my context that I use to configure Spring:</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt;
   &lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
  &lt;context:component-scan base-package="com.project.do" /&gt;
  
  &lt;!--&lt;mvc:annotation-driven/&gt; --&gt;
  
  &lt;bean class="com.project.do.interceptors.handler.mapping.HandlerInterceptorAnnotationAwareHandlerMapping "/&gt;
  
  &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /&gt;
  
  &lt;bean class="com.project.do.viewresolver.ProjectViewResolver" &gt;
      &lt;property name="tilesResolver" ref="tilesResolver" /&gt;
      &lt;property name="jspResolver" ref="jspResolver" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="tilesResolver" class ="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt;
      &lt;property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" /&gt;
      &lt;property name="order" value="2" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jspResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="prefix" value="/WEB-INF/jsp/" /&gt;
      &lt;property name="suffix" value=".jsp" /&gt;
      &lt;property name="order" value="3" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id ="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"&gt;
      &lt;property name="definitions" value="/WEB-INF/tiles/tiles.xml" /&gt;
      &lt;property name="preparerFactoryClass" value="org.springframework.web.servlet.view.tiles2.SpringBeanPreparerFactory"/&gt;
  &lt;/bean&gt;
  
  &lt;mvc:resources mapping="/css/**" location="/css/" /&gt;
  &lt;mvc:resources mapping="/js/**" location="/js/" /&gt;
  &lt;mvc:resources mapping="/images/**" location="/images/" /&gt;
  &lt;mvc:resources mapping="/javascript/**" location="/javascript/" /&gt;
  &lt;mvc:resources mapping="/scripts/**" location="/scripts/" /&gt;
  &lt;mvc:resources mapping="/styles/**" location="/styles/" /&gt;
  &lt;mvc:resources mapping="/help/**" location="/help/" /&gt;
  
  &lt;!-- &lt;mvc:view-controller path="/" view-name="welcome"/&gt; --&gt;
  </code></pre>
  
  <p></p>
  
  <p>I should note that I have a working Controller with Ajax in the package com.project.do.working, but the nonworking Controller with Ajax and JSON is in the same package com.project.do.not.working</p>