Search code examples
javaangularjsjsonspringangular-resource

Angular code doesn't delete json by passing id to JAVA (with Spring)


My application should delete one Session object by using ID, that I pass from Angular to Java class by using Spring mapping. It doesn't quite work. Nothing happens if done from UI. I checked the logs in browser: no errors. Then i check the logs in Java and it appears that angular didn't reach Java.

If I use from browser address bar: rest/session/delete/IDNUMBEROFSESSION, then it works and deletes the data (although after returns HTTP Status 404 -). Can anybody please suggest what I am doing wrong? Thank you very much!!!

SessionResource.java

@Controller
@RequestMapping("/session")
public class SessionResource {

  private static Logger LOG = LoggerFactory.getLogger(SessionResource.class);

  @Autowired
  private SessionService sessionService;

  @RequestMapping(method = RequestMethod.GET, value = "/{id}")
  public ResponseEntity<Session> get(@PathVariable String id) {
    Session session = sessionService.findById(id);
    return new ResponseEntity<Session>(session, HttpStatus.ACCEPTED);
  }

  // DELETE
  @RequestMapping(method = RequestMethod.GET, value = "/delete/{id}")
  public void delete(@PathVariable("id") String id) {
    System.out.println("Starting to enter request mapping delete and calling sessionService.delete function");
    Session session = sessionService.findById(id);
    sessionService.delete(session);
  }
}

Resource.JS with this code:

app.factory('SessionResource', function ($resource) {
    return $resource('rest/session/:sessionId',
        {
            speakerId: '@sessionId'
        },
        {
            'update': { method: 'PUT' },
            'delete': { method: 'GET' ,  isArray: true}
        });
});

Solution

  • You need to call delete method of SessionResource by passing sessionId parameter to it.

    SessionResource.delete({sessionId: data.id}, function(response){
       callback(response);
    })