Basics
Creating a REST microservice with Springboot couldn't be easier. Simply include the spring-boot-starter-web artifact, add a @RestController annotation, sprinkle some @RequestMapping annotations and you're done. Other Considerations While the above statement is somewhat accurate, the truth is that there is a lot more to think about. For instance:
Versioning In its simplest form, versioning is accomplished through the URI. An example would look like: http://hostname:port/v1.0/resource The general rule is that the version number must change whenever modifications to the API require changes to the client. There is also the concept of consumer driven contracts which is covered using cloud.spring.io/spring-cloud-contract/. Richardson Maturity Model If you are not familiar with this model, please refer to this link for details: martinfowler.com/articles/richardsonMaturityModel.html Level 0 - This is attained by simply using the @RestController annotation. It will provide http communication. Level 1 - Is achieved by using @RequestMapping(value = "resource"). This will provide us with a resource specific uri, i.e. http://hostname:port/resource Level 2 By adding to the RequestMapping annotation, we can specify the verb associated with the resource as such: @RequestMapping(value = RESOURCE, method = RequestMethod.POST) We also need to provide the proper return codes which can be accomplished by either using @ResponseStatus or returning the status in the ResponseEntity:
// Set the location for the new object HttpHeaders headers = new HttpHeaders(); URI locationUri = ucb.path("/v1.0/resource/").path(id).build().toUri(); headers.setLocation(locationUri); Level 3 - This uses the linkto method. For details, please refer to spring.io/guides/gs/rest-hateoas/ Exception Handling Exceptions can be handled many different ways in Spring. It really is a matter of preference. The method I prefer is to allow my rest controllers to throw exceptions and then have them handled through a @ControllerAdvice class. This class would then contain methods for each of the exceptions we want to handle. Here is an example: @Component @ControllerAdvice public class BaseExceptionHandler { /** * Exception handler that will handle anything that derives from the * ResourceNotFoundException class. It will return a HTTP 404. * * @param e is the Exception object containing the exception thrown * @return RestError containing the exception message */ @ResponseStatus(HttpStatus.NOT_FOUND) @ExceptionHandler(value = ResourceNotFoundException.class) public RestError handleNotFoundException(ResourceNotFoundException e) { final RestError restError = new RestError(); restError.setMessage(e.getMessage()); return restError; } /** * Exception handler that will handle anything that derives from the * Exception class. It will return a HTTP 400 for all exceptions * * @param e is the Exception object containing the exception thrown * @return RestError containing the exception message */ @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(value = Exception.class) public RestError handleBaseException(Exception e) { final RestError restError = new RestError(); restError.setMessage(e.getMessage()); return restError; } } Inputs and Outputs When it comes to handling inputs and outputs, Spring does most of the work. Want your POST method to accept a json string representing one of your classes? Simply add consumes="applicaiton/json" to your request mapping and a @RequestBody parameter and Spring will take care of the rest. Outputs are just as easy. To return an object in json format, just add produces="application/json" to your request mapping and a @ResponseBody annotation. The object you return will be converted to json for you. Next we will be talking about Spring Cloud and how we can help tie it all together. Comments are closed.
|
Visit our Code Repository for samples and demos.
ARTICLES
All
|