1. Send error message to client
@RequestMapping (value = "/{id}", method = requestmethod.get) public @ResponseBody spittle Spittlebyid (@PathVariable Long id) { return spittlerepository.findone (ID);}
The above method is to find a Spittle object.
If it is not found, then NULL is returned, when the HTTP status code returned to the client is (OK), which indicates that the event is working, but the data is empty.
In this case, we hope that the status code may not be 404 (not Found), so the client will know what error has occurred.
To implement this functionality, Spring provides several ways:
A. Use @responsestatus annotations to specify status codes
B, Controller method can return Responseentity object, change object can contain more response related metadata
C, the exception processor can handle the error scenario, so that the processor method can focus on the normal situation
Using responseentity
Using Responseentity objects instead of @responsebody
@RequestMapping (value = "/{id}", method = requestmethod.get) public responseentity<spittle> Spittlebyid (@PathVariable Long id) { return spittlerepository.findone (ID); null ? HttpStatus.OK:HttpStatus.NOT_FOUND; return New Responseentity<spittle>(spittle, status);}
With responseentity, you can specify an HTTP status code, and it also contains the definition of @responsebody, which is equivalent to using @responsebody.
Next, we want to return an error message if the Spittle object is not found
The
can create an error class and then use generics to return an Error object when the Spittle object is not found
@RequestMapping (value = "/{id}", Method = Requestmethod.get) public responseentity<?> Spittlebyid (@PathVariable Long ID) { return Spittlerepository.findone (id ); if (spittle = = null " {Error error = new error (4, "spittle [" + ID + "] not Found "); return new responseentity< Error> (error, httpstatus.not_found); return new responseentity< Spittle>
However, this kind of writing seems to make the code a little more complicated, we can consider using the wrong processor.
Handling Errors
Step 1: Create an Exception class
Public classSpittlenotfoundexceptionextendsRuntimeException {Private Static Final LongSerialversionuid = 1L; Private LongSpittleid; PublicSpittlenotfoundexception (LongSpittleid) { This. Spittleid =Spittleid; } Public LongGetspittleid () {returnSpittleid; }}
Step 2: Create a processing method for the error handler under the Controller
@ExceptionHandler (spittlenotfoundexception. Class) public resopnseentity<error> spittlenotfound (spittlenotfoundexception e) { long Spittleid = E.getspittleid (); = New Error (4, "spittle [" + Spittleid + "] not found"); return New Responseentity<error>(Error, Httpstatus.not_found);}
@ExceptionHandler annotations are added to the Controller method to handle the corresponding exception.
If request a has an exception and is caught by this method, the return value of the request is the return value of the exception handler.
Step 3: The original business controller method can be simplified
@RequestMapping (value = "/{id}", method = requestmethod.get) public responseentity<spittle> Spittlebyid (@PathVariable Long id) { return spittlerepository.findone (ID); if NULL Throw New spittlenotfoundexception (ID);} return New Responseentity<spittle>(spittle, Httpstatus.ok);}
And because at any time, this control method has data return, so the HTTP status code is always 200, so you can not use responseentity, two use @responsebody; if @restcontroller is used on the controller, We can save @responsebody and finally get the code.
@RequestMapping (value = "/{id}", method = requestmethod.get) public spittle Spittlebyid (@ Pathvariable Long ID) { return spittlerepository.findone (ID); if NULL Throw New spittlenotfoundexception (ID);} return spittle;}
Step 4: Similarly, you can simplify the error handler
@ExceptionHandler (spittlenotfoundexception. Class) @ResponseStatus (httpstatus.not_found) public Error Spittlenotfound ( Spittlenotfoundexception e) { long Spittleid = E.getspittleid (); return New Error (4, "spittle [" + Spittleid + "] not found");}
In the simplification process, we all want to avoid using responseentity, because he can cause the code to look very complicated.
However, some situations must use responseentity to implement certain functions.
Setting header information in a response
To use the responseentity, for example, after I create a new spittle, the system returns the URL resource address of this spittle through the Location header information of HTTP.
There's no discussion here.
"Spring MVC" 16.3, providing content other than resources