restexceptionstruts2http-status-code-500struts2-rest-plugin

Need to Send HTTP-error-500 when an Exception occurs


I have designed a RESTful backend using struts2-rest-plugin. My front-end is Ember.js CRUD app.

When I try to create a new record with duplicate ID my server is showing an exception.

How do I send it as HTTP-error-code-500 to my ember.js app?


Solution

  • In case of Struts I just needed to return DefaultHttpHeaders.

     public HttpHeaders create() {
            try{
                employeesService.save(model);
            }
            catch(Exception e){
                System.out.println(e);
                return new DefaultHttpHeaders("DuplicateId").withStatus(409);
            }
            return new DefaultHttpHeaders("success")
                .setLocationId(model.getId());
        }