title: angularjs + spring mvc + json post request
body: <p>I've been doing some research on the web regarding my problem, and I feel that my code reflects what's written in most of the examples - and yet, it still doesn't work. Thus my question.</p>
  
  <p>Long story short, I want to send POST request to add new item to some "backend". For REST API I use Spring MVC, for sending the request I use $http from AngularJS</p>
  
  <p>That's how I invoke the POST request</p>
  
  <pre><code>    $scope.testAddItem = function(){
      $http({
          'url' : 'addNewItem',
          'method' : 'POST',
          'headers': {'Content-Type' : 'application/json'},
          'data' : $scope.newItem
      }).success(function(data){
          $scope.marketForm.texts.push({'text' : data.text});
      })
  }
  </code></pre>
  
  <p>Which maps to the proper URL. Problem is that I get the following response</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/&gt;
  &lt;title&gt;Error 400 BAD_REQUEST&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;&lt;h2&gt;HTTP ERROR 400&lt;/h2&gt;
  &lt;p&gt;Problem accessing /poll/addNewItem/. Reason:
  &lt;pre&gt;    BAD_REQUEST&lt;/pre&gt;&lt;/p&gt;&lt;hr /&gt;&lt;i&gt;&lt;small&gt;Powered by Jetty://&lt;/small&gt;&lt;/i&gt;&lt;br/&gt; 
  </code></pre>
  
  <p>I get this error either when I click the button on the website, and when I'm testing it via some REST Client.</p>
  
  <p>Dependencies I've included in my pom.xml</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-core&lt;/artifactId&gt;
          &lt;version&gt;2.2.3&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
          &lt;version&gt;2.2.3&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-annotations&lt;/artifactId&gt;
          &lt;version&gt;2.2.3&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.codehaus.jackson&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-mapper-asl&lt;/artifactId&gt;
          &lt;version&gt;1.9.13&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>One note - sending other request via GET works.</p>
  
  <p>My Controller class</p>
  
  <pre><code>@Controller
  @RequestMapping(value = "/")
  public class PollController {
  
  @RequestMapping(method = RequestMethod.GET)
  public String mainPage(){
      return "poll";
  }
  
  @RequestMapping(value = "/getNewElement")
  public @ResponseBody
  List&lt;PollItem&gt; getNewElement(){
  
      List&lt;PollItem&gt; listOfItems = new ArrayList&lt;PollItem&gt;(2);
  
      listOfItems.add(new PollItem("test 1"));
      listOfItems.add(new PollItem("test 2"));
  
      return listOfItems;
  }
  
  @RequestMapping(value = "/addNewItem", method = RequestMethod.POST, consumes =    MediaType.APPLICATION_JSON_VALUE)
  public @ResponseBody PollItem addNewElement(@RequestBody PollItem pollItem){
      pollItem.setId(2); // I know it makes no logical sense, but it's just for testing.
      return pollItem;
  }
  
  }
  </code></pre>
  
  <p>Any clues ?</p>
  
  <p>EDIT</p>
  
  <p>After adding debug mode it seems that it's some JSON parsing problem</p>
  
  <pre><code>Method [public com.poll.model.PollItem com.poll.controller.PollController.addNewElement(com.poll.model.PollItem)]
  
      org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unexpected character ('a' (code 97)): expected a valid value (number, String, arr
      ay, object, 'true', 'false' or 'null')
      at [Source: HttpInputOverHTTP@5ce15706; line: 1, column: 2]; nested exception is org.codehaus.jackson.JsonParseException: Unexpected character ('a' (code 97)): expected
      a valid value (number, String, array, object, 'true', 'false' or 'null')
      at [Source: HttpInputOverHTTP@5ce15706; line: 1, column: 2]
      at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:187)
      at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.read(MappingJacksonHttpMessageConverter.java:179)
      at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodAr
      gumentResolver.java:138)
      at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:183)
  
      at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:98)
      at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79)
      at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
      at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
      at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:738)
      at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:551)
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
      at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:568)
      at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221)
      at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1111)
      at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:478)
      at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183)
      at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1045)
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
      at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:199)
      at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:109)
      at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
      at org.eclipse.jetty.server.Server.handle(Server.java:462)
      at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:279)
      at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:232)
      at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:534)
      at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607)
      at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536)
      at java.lang.Thread.run(Thread.java:724)
      Caused by: org.codehaus.jackson.JsonParseException: Unexpected character ('a' (code 97)): expected a valid value (number, String, array, object, 'true', 'false' or 'null'
      )
      at [Source: HttpInputOverHTTP@5ce15706; line: 1, column: 2]
      at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1433)
      at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:521)
      at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:442)
      at org.codehaus.jackson.impl.Utf8StreamParser._handleUnexpectedValue(Utf8StreamParser.java:2090)
      at org.codehaus.jackson.impl.Utf8StreamParser._nextTokenNotInObject(Utf8StreamParser.java:606)
      at org.codehaus.jackson.impl.Utf8StreamParser.nextToken(Utf8StreamParser.java:492)
      at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2770)
      at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2718)
      at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1923)
      at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:184)
      ... 38 more
  </code></pre>
  
  <p>EDIT2</p>
  
  <p>It seems that it works, problem is that it supports only " (double quotes) where I want to use single-quotes. Is there any reason why I should double quotes always when doing JSON?</p>
  
  <p>And what I should do change default behaviour of JSON mapper?</p>