title: 500 internal server error in spring mvc 3 controller response for ajax call
body: <p>my code gives 500 error when I have the following code. It sends a jason response to an ajax call.</p>
  
  <pre><code>    @RequestMapping(value = "/savecourse", method = RequestMethod.POST)
  public @ResponseBody
  Object saveLecturer(@Valid @ModelAttribute(value = "course") Course course,
          BindingResult result) {
  
      Map&lt;String, Object&gt; response = new HashMap&lt;String, Object&gt;();
  
      if (result.hasErrors()) {
          List&lt;ObjectError&gt; results = result.getAllErrors();
          for (ObjectError objectError : results) {
              System.out.println(objectError.getDefaultMessage());
          }
          response.put("message", "Could not add the Course to the system.");
      } else {
          try {
              course.setId(courseDao.saveCourse(course));//returns the id
              response.put("course", course);
  
          } catch (Exception e) {
              System.out.println(e);
  
          }
      }
  
      return response;
  }
  </code></pre>
  
  <p>But when I create a new object and copy the parameters to the other object, it works fine. The second method(Not a good method of course) works well. All the parameters in the request object are set to the cse object as well.</p>
  
  <pre><code>    @RequestMapping(value = "/savecourse", method = RequestMethod.POST)
  public @ResponseBody
  Object saveLecturer(@Valid @ModelAttribute(value = "course") Course course,
          BindingResult result) {
  
      Map&lt;String, Object&gt; response = new HashMap&lt;String, Object&gt;();
  
      if (result.hasErrors()) {
          List&lt;ObjectError&gt; results = result.getAllErrors();
          for (ObjectError objectError : results) {
              System.out.println(objectError.getDefaultMessage());
          }
          response.put("message", "Could not add the Course to the system.");
      } else {
          try {
              course.setId(courseDao.saveCourse(course));//returns the id
              Course cse = new Course();
              cse.setId(course.getId());
              cse.setCourseName(course.getCourseName());
              cse.setFee(course.getFee());
              Lecturer lec = new Lecturer();
              lec.setId(course.getLecturer().getId());
              lec.setFirstName(course.getLecturer().getFirstName());
              lec.setLastName(course.getLecturer().getLastName());
              cse.setLecturer(lec);
              cse.setGrade(course.getGrade());
              response.put("course", cse);
  
          } catch (Exception e) {
              System.out.println(e);
  
          }
      }
  
      return response;
  }
  </code></pre>
  
  <p>Can you please tell me what is wrong in the first method? 
  Any help is greatly appreciated.</p>