1

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.

Here is the controller code:

@RestController
public class SpringRestController {
    
@Autowired
private CourseService courseService;

//Get the courses
@GetMapping("/courses")
public List<Course> getCourses()
{
    return this.courseService.getCourses();
}

@GetMapping("/courses/{courseId}")
public Course getCourse(@PathVariable String courseId)
{
    return this.courseService.getCourse(Long.parseLong(courseId));
}

//Add a course
@PostMapping("/courses")
public Course addCourse(@RequestBody Course course)
{
    return this.courseService.addCourse(course);
}

@PutMapping("/courses/{courseId}")
public Course updateCourse(@PathVariable String courseId,@RequestBody Course course)
{
    return this.courseService.updateCourse(Long.parseLong(courseId),course);
}

@DeleteMapping("/courses/{courseId}")
public List<Course> deleteCourse(@PathVariable String courseId)
{
    return (List<Course>) this.courseService.deleteCourse(Long.parseLong(courseId));    
    
}
}

Here is the service implementation of the request :

@Service
public class CourseServiceImpl implements CourseService {

List<Course> list;

public CourseServiceImpl()
{
    list = new ArrayList<>();
    list.add(new Course(145l,"Java Array","Basic Array"));
    list.add(new Course(123l,"Java Constructor","Basic Constructor"));
}

@Override
public List<Course> 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<Course> deleteCourse(long courseId) {
    
    for(Course course:list)
    {
        if(course.getId()==courseId)
        {           
            list.remove(course);
        }
    }       
    return list;        
}
}

No errors in spring boot.

Error I got in postman is here :

{
"timestamp": "2021-07-13T03:36:27.454+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/courses/786"
}
18
  • Can you ping the errors Commented Jul 13, 2021 at 3:55
  • After deletion also we can return whatever. Commented Jul 13, 2021 at 3:55
  • 1
    There's not enough information in your question for anyone to help you. Please post the error text from postman and any logs from your server. Have a look at how to ask and or how to create a minimal reproducible example. Commented Jul 13, 2021 at 3:57
  • This is the error in postman : { "timestamp": "2021-07-13T03:36:27.454+00:00", "status": 500, "error": "Internal Server Error", "path": "/courses/786" } Commented Jul 13, 2021 at 3:58
  • This error from postman ping the error from console. Commented Jul 13, 2021 at 3:59

4 Answers 4

2

According to your Code, foreach cannot used to remove item, you should use Iterator.

for (Iterator<Course> iterator = list.iterator(); iterator.hasNext(); ) {
    Course course = iterator.next();
    if (course.getId() == courseId) {
        iterator.remove();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I need at least 15 reputations to cast a vote. But Thank you very much sir. Can you tell me the reason why we this method?
1

Remove the object using index not like object. That the issue.

list.remove(index of the object);


Iterator<Object> li = list;

While(li.hasNext()){
 Object obj= li.next();
 If(courseId==id){
 Ii.remove();
 }
}

1 Comment

I need at least 15 reputations to cast a vote. But Thank you very much sir.
0

You are trying to pass courseId as "786" which is not present in your ArrayList. If you still want to delete it, then add a course with courseId "786" in your Course list.

When I tried deleting course with id 786 it shows all other courses because courseId 786 is not present in the list.

Comments

-1

Check course id 786 is present in your list or not.

2 Comments

I implemented an post http request also that's workiing fine. I added course with 786 course id , then I was trying to delete the same course with id 786 just to check if its deleting or not
If courseId present or not that's not issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.