Springboot – Returning HTTP response code based on ResponseEntity
Each http request can have a different response code sent by http server according to the processing result. The response code has to be used properly, according to the semantic defined for each content.
· 1xx: Information
· 2xx: Success
· 3xx: Redirect
· 4xx: Client error
· 5xx Server error
@RequestMapping(value="/update_author/{id}/{first_name}", produces = { "application/json" })
@Transactional
public ResponseEntity update(@PathVariable(value="first_name") String firstName, @PathVariable(value="id") Long id ) {
String
queryStr="update actor set
first_name =? where actor_id=?";
int result=-1;
Query
query=entityManager.createNativeQuery(queryStr);
query.setParameter(1, firstName);
query.setParameter(2, id);
result=query.executeUpdate();
if(result==0) {
HttpStatus.Metho
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
return new ResponseEntity(HttpStatus.OK);
}
You can
download the whole source code on github:
https://github.com/rafaelqg/code/blob/main/springboot_responseEntity.java
You can see a video class about this theme here:
Comments
Post a Comment