Note that there are some explanatory texts on larger screens.

plurals
  1. POIn Spring MVC, how to gracefully handle Validate.notNull IllegalArgumentException when a "get by id" finds no item?
    primarykey
    data
    text
    <p>I have a Spring MVC web app and use Hibernate for persistence. </p> <p>I have a table of vehicles. Each vehicle has a unique ID.</p> <p>One webpage in my app lists all of the vehicles (www.example.com/vehicles). Clicking on a link takes you to the details page of that vehicle via a GET parameter of the vehicle's ID (www.example.com/vehicles?id=1234).</p> <p>This is all fine because if a vehicle gets deleted from the table, that vehicle no longer appears on the page that lists all vehicles, so there is no link to click.</p> <p><strong>However, if someone supplies an id of a vehicle that does not exist (e.g. they had bookmarked the path of a particular vehicle but that vehicle has since been deleted--e.g. www.example.com/vehicles?id=1234) and that vehicle is no longer in the database, there is an exception.</strong></p> <pre><code>Vehicle vehicle = vehicleDAO.findById(id); Validate.notNull(vehicle, "There is no vehicle by this ID."); </code></pre> <p><strong>My controller will reach these couple lines and will throw an IllegalArgumentException on the line with Validate.notNull. How can I code a site-wide handler for gracefully telling a website visitor the message that appears in that Validate.notNull statement?</strong></p> <p>I think Validate.notNull statements are useful throughout my code, and I'd like to send their messages all the way to the browser window so that the visitor doesn't just see some generic 500 Error page.</p> <p>This applies not just to my Vehicles table but to anything.</p> <p>It would be nice to have just one bit of code that gracefully handles this problem for all controllers throughout the site.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload