Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7853607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:44:54+00:00 2026-06-02T19:44:54+00:00

How do I correctly use RESTful delete in Spring MVC controller? I have DAO

  • 0

How do I correctly use RESTful delete in Spring MVC controller? I have DAO that returns boolean when trying to delete some item.

I am trying to delete item. If everything was OK, just show list of items (deleted item won’t be there anymore). If item cannot be removed, redirect to details page and say why it couldn’t be deleted.

Do I need some special response status or something like this? Is my approach RESTful?

@RequestMapping(value = "items/{id}", method = RequestMethod.DELETE)
public String delete(@PathVariable("id") int itemId, Model model) {
    Item item = itemDao.get(id);

    // true -> can delete
    // false -> cannot delete, f.e. is FK reference somewhere
    boolean wasOk = itemDao.delete(item); 

    if (wasOk) {
        return "redirect:/items";
    }

    // will write to user which item couldn't be deleted
    model.addAttribute("item", item);
    return "items/error";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:44:56+00:00Added an answer on June 2, 2026 at 7:44 pm

    If you the issues with a delete can be fixed by the user, then this seems ok. If there is nothing the user can do then perhaps an error code status would be more correct. The only failure I can imagine for a deletion would be an authorization failure, which would be a 401. This could be set by adding a parameter to your method ‘HttpServletResponse response’. Your code would become something like:

    @RequestMapping(value = "items/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable("id") int itemId, Model model, HttpServletReponse response) {
        Item item = itemDao.get(id);
    
        // true -> can delete
        // false -> cannot delete, f.e. is FK reference somewhere
        boolean wasOk = itemDao.delete(item); 
    
        if (!wasOk) {
            // will write to user which item couldn't be deleted
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            model.addAttribute("item", item);
            return "items/error";   
        }
    
        return "redirect:/items";
    }
    

    You can substitue other status codes as appropriate, but that is the General idea.

    You could also do something like:

        if (!wasOk) {
            throw new DataAccessException("Unable to delete item: " + item);
        }
    

    And then have an annotated error handler in the same class

    @ExceptionHandler(DataAccessException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public String handleDataAccessException(DataAccessException ex) {
        // Do some stuff
        return "errorView";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are trying to build some RESTful services with Spring MVC. We will be
I have a controller action that takes an id as a parameter. The restful
I have a rails site that allows authentication through OAuth (I use Restful Authentication
When I use HelpBroker.setCurrentID([some help id]) , JavaHelp correctly shows the page I want
I'm trying to figure out how to use Zend_Db_Table_Abstract correctly. I want to return
I have a RESTful Rails application with a resource called Foo. I'm trying to
I am currently trying to run some tests on a RESTful web service and
I have a web application that uses RESTful url patterns. Currently if a users
I want to add user authentication to my restful api that I have created
I'm trying to use Jersey as a client for a RESTful application. Specifically I'd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.