title: REST API delete HTTP request in Spring Boot
body: <p>I'm learning REST API. I'm trying to delete an element from the list. I tried but getting an error on the postman. Can anyone help me where I went wrong?
  Also, can we return the object after deleting it? I also tried it but I think I'm messing up in delete code. So it was not working.</p>
  <p><strong><strong>Here is the controller code:</strong></strong></p>
  <pre><code>@RestController
  public class SpringRestController {
      
  @Autowired
  private CourseService courseService;
  
  //Get the courses
  @GetMapping(&quot;/courses&quot;)
  public List&lt;Course&gt; getCourses()
  {
      return this.courseService.getCourses();
  }
  
  @GetMapping(&quot;/courses/{courseId}&quot;)
  public Course getCourse(@PathVariable String courseId)
  {
      return this.courseService.getCourse(Long.parseLong(courseId));
  }
  
  //Add a course
  @PostMapping(&quot;/courses&quot;)
  public Course addCourse(@RequestBody Course course)
  {
      return this.courseService.addCourse(course);
  }
  
  @PutMapping(&quot;/courses/{courseId}&quot;)
  public Course updateCourse(@PathVariable String courseId,@RequestBody Course course)
  {
      return this.courseService.updateCourse(Long.parseLong(courseId),course);
  }
  
  @DeleteMapping(&quot;/courses/{courseId}&quot;)
  public List&lt;Course&gt; deleteCourse(@PathVariable String courseId)
  {
      return (List&lt;Course&gt;) this.courseService.deleteCourse(Long.parseLong(courseId));    
      
  }
  }
  </code></pre>
  <p><strong>Here is the service implementation of the request :</strong></p>
  <pre><code>@Service
  public class CourseServiceImpl implements CourseService {
  
  List&lt;Course&gt; list;
  
  public CourseServiceImpl()
  {
      list = new ArrayList&lt;&gt;();
      list.add(new Course(145l,&quot;Java Array&quot;,&quot;Basic Array&quot;));
      list.add(new Course(123l,&quot;Java Constructor&quot;,&quot;Basic Constructor&quot;));
  }
  
  @Override
  public List&lt;Course&gt; getCourses() {
      return list;
  }
  
  @Override
  public Course getCourse(long courseId) {
      Course c = null;
      for(Course course:list)
      {
          if(course.getId()==courseId)
          {
              c=course;
              break;
          }
      }
      return c;
  }
  
  @Override
  public Course addCourse(Course course) {
      list.add(course);
      return course;
  }
  
  @Override
  public Course updateCourse(long courseId,Course course) {
      Course c = null;
      for(Course cour:list)
      {
      if(cour.getId()==courseId)
      {
          cour.setTitle(course.getTitle());
          cour.setDescription(course.getDescription());
          c=cour;
      }
      }   
      return c;       
  }
  
  @Override
  public List&lt;Course&gt; deleteCourse(long courseId) {
      
      for(Course course:list)
      {
          if(course.getId()==courseId)
          {           
              list.remove(course);
          }
      }       
      return list;        
  }
  }
  </code></pre>
  <p><strong>No errors in spring boot.</strong></p>
  <p><strong>Error I got in postman is here :</strong></p>
  <pre><code>{
  &quot;timestamp&quot;: &quot;2021-07-13T03:36:27.454+00:00&quot;,
  &quot;status&quot;: 500,
  &quot;error&quot;: &quot;Internal Server Error&quot;,
  &quot;path&quot;: &quot;/courses/786&quot;
  }
  </code></pre>