title: Getting http 400 Bad Request Error with spring and resttemplate postForObject
body: <p>I keep getting Bad Request  HttpClient 400 Exception when executing the POST call to my Webservice.
  I cant figure out wether the error is originated on client or server side ... pls help me to track down this error. But since the pst call with curl is working fine i supspect that the error origins on the client side with  the construction of springs resttemplate</p>
  
  <p>The GET call is working fine!
  Also the post call with curl is working fine</p>
  
  <pre><code>C:\Users\pk&gt;curl -v  -H "Accept: application/json" -H "Content-Type: application
  /json" -X POST http://127.0.0.1:8080/TblGps/update -d @json.txt
  </code></pre>
  
  <p>Appriciate if someone can point out where i got it wrong.</p>
  
  <p><strong>my client code is:</strong></p>
  
  <p>when is comes to resttemplate.postForObject ....  a Http 400 Bad Request Exception is thrown - the GET is working fine and the curl command line call with post is also working</p>
  
  <pre><code>    private class HttpRequestTask extends AsyncTask&lt;Void, Void, TblGps&gt; {
          @Override
          protected TblGps doInBackground(Void... params) {
              try {
                  final String urlGET = "http://10.0.2.2:8080/TblGps/get?id=1";
                  final String urlPOST = "http://10.0.2.2:8080/TblGps/update/";
  
  
                  RestTemplate restTemplate = new RestTemplate();
                  restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
  
                  //get Object from Service
                  TblGps gps = (TblGps) restTemplate.getForObject(urlGET,TblGps.class);
  
  
                  // alter the Object Data
                 gps.setDescr("success");
  
                //POST Object to Service Endpoint
                TblGps gpsResult = restTemplate.postForObject(urlPOST, "POST", TblGps.class, gps);
  
  
               return gpsResult;
  
  
              } catch (Exception e) {
                  Log.e("REST call", e.getMessage(), e);
              }
  
              return null;
          }
  }
  }
  </code></pre>
  
  <p>and i've also tried with restTemplate.postForEntity</p>
  
  <pre><code> HttpHeaders headers = new HttpHeaders();
  
              headers.add("Accept", "application/json");
              headers.add("Content-Type", "application/json");
      HttpEntity ent = new HttpEntity(gps,headers);
            ResponseEntity&lt;TblGps&gt; out  = restTemplate.postForEntity(urlPOST, HttpMethod.POST, TblGps.class,ent);
  </code></pre>
  
  <p><strong>my controller code:</strong></p>
  
  <pre><code>  @RequestMapping(value="/TblGps/update", method=RequestMethod.POST, consumes = "application/json",produces="application/json")
    @ResponseBody public TblGps post(@RequestBody  TblGps gps){
  
          logger.debug("/TblGps/update: " + gps.getId());
            System.out.println("save.............."+ gps);
            return Application.DataRepository.save(gps);
      }
  </code></pre>
  
  <p><strong>my console log:</strong> the mapping of the Endpoints looks also ok  on console log</p>
  
  <pre><code>2014-05-14 08:15:37.850  INFO 2280 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  2014-05-14 08:15:41.457  INFO 2280 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/TblGps/List],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.List&lt;com.pekam.TblGps&gt; com.pekam.TblGpsController.findAll()
  2014-05-14 08:15:41.464  INFO 2280 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/TblGps/get],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public com.pekam.TblGps com.pekam.TblGpsController.findById(long)
  2014-05-14 08:15:41.471  INFO 2280 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/TblGps/update],methods=[POST],params=[],headers=[],consumes=[application/json],produces=[application/xml || application/json],custom=[]}" onto public com.pekam.TblGps com.pekam.TblGpsController.post(com.pekam.TblGps)
  2014-05-14 08:15:41.478  INFO 2280 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/*],methods=[GET || POST || DELETE || PUT || HEAD],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.pekam.TblGpsController.allFallback()
  2014-05-14 08:15:42.701  INFO 2280 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  2014-05-14 08:15:42.706  INFO 2280 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  2014-05-14 08:15:56.645  INFO 2280 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
  2014-05-14 08:15:57.551  INFO 2280 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http
  2014-05-14 08:15:57.665  INFO 2280 --- [           main] com.pekam.Application                    : Started Application in 46.091 seconds (JVM running for 46.681)
  </code></pre>
  
  <p>stacktrace of exception:</p>
  
  <pre><code>400 Bad Request
  : org.springframework.web.client.HttpClientErrorException: 400 Bad Request
      at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:76)
      at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:524)
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:481)
      at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
      at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:317)
      at com.pekam.myandroid.MainActivity$HttpRequestTask.doInBackground(MainActivity.java:106)
      at com.pekam.myandroid.MainActivity$HttpRequestTask.doInBackground(MainActivity.java:1)
      at android.os.AsyncTask$2.call(AsyncTask.java:288)
      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
      at java.lang.Thread.run(Thread.java:841)
  </code></pre>
  
  <p>here is my code of the data class:</p>
  
  <pre><code>/**
   * The persistent class for the tblGps database table.
   * 
   */
  @Entity
  @Table(name="tblGps")
  @NamedQuery(name="TblGps.findAll", query="SELECT t FROM TblGps t")
  public class TblGps implements Serializable {
      private static final long serialVersionUID = 1L;
      private long id;
      private Timestamp date ;
      private String descr;
      private int gpsCordsX;
      private int gpsCordsY;
      private int userid;
  
      @SuppressWarnings("deprecation")
      public TblGps() {
  
  
          date= new Timestamp(0,0,0,0,0, 0, 0);
      }
  
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(unique=true, nullable=false)
      public long getId() {
          return this.id;
      }
  
      public void setId(long id) {
          this.id = id;
      }
  
  
      @Column(nullable=false)
      public Timestamp getDate() {
          return this.date;
      }
  
      public void setDate(Timestamp date) {
          this.date = date;
      }
  
  
      public String getDescr() {
          return this.descr;
      }
  
      public void setDescr(String descr) {
          this.descr = descr;
      }
  
  
      @Column(nullable=false, precision=53)
      public int getGpsCordsX() {
          return this.gpsCordsX;
      }
  
      public void setGpsCordsX(int gpsCordsX) {
          this.gpsCordsX = gpsCordsX;
      }
  
  
      @Column(nullable=false, precision=53)
      public int getGpsCordsY() {
          return this.gpsCordsY;
      }
  
      public void setGpsCordsY(int gpsCordsY) {
          this.gpsCordsY = gpsCordsY;
      }
  
  
      @Column(nullable=false)
      public int getUserid() {
          return this.userid;
      }
  
      public void setUserid(int userid) {
          this.userid = userid;
      }
  
  }
  </code></pre>