In this talk, I walk you through building a RESTful API using Kotlin & Spring. You will learn how to build a web service, setup error handling & render documentation for Swagger, as well as, the importance of API contracts.
working on B2B Apps and SDKs. I have been working with Android since late 2010. When I am not writing code, I spend my time with my wife, snowboarding, reading books, attending events and writing articles for my own blog at www.kdotj.com. I am also on the Android Tutorial team Author @ RayWenderlich.com And on the Flutter Tutorial team as an Author @kandroidj 2 SPEAKER
codes, verbs, etc. 2. API Contracts 3. SpringBoot with Kotlin 4. Exception Handling 5. Documentation & Testing with Postman 6. Deployment to Elastic Beanstalk on AWS 7. Wrapping it all up with a CI build that uses the API 3 CONCEPTS
for our API users Client 400s Client Error 400 codes are reserved for client errors Server 500s Server Error 500 codes are reserver for server errors OK 200s Success 200 codes are reserved for success scenarios 100 status codes are used for information scenarios. 300 status codes are used for redirection scenarios. 4
PUT - Update/Replace POST - Create PATCH - Update/Modify DELETE - Delete Photo by Road trip with Raj on unsplash Photo by Aaron Burden on unsplash Photo by NeONBRAND on unsplash
Collections in API development are lists of a type of resource. For example, consider a Shapes API. We can use it to request a list of shapes or a single shape. A Collection is the list and the single shape is a resource. Getting a resource: GET api.shapes.com/shapes/1 - returns a single resource, the square GET api.shapes.com/shapes - returns a collection of shapes
an agreement about the features and functionality of an application programming interface. In the case of RESTful APIs, it is the endpoints and response objects returned from them. Photo by Cytonn Photography on unsplash
and @ResponeBody annotations into one. @Controller - a specialization on the Component classes and allows implementation classes to be auto detected @ResponseBody - maps the http response body to a transfer or domain object 11 BUILDING A REST API
easier to define a GET based request with a path. Alternatively, we could use @RequestMapping but the path and GET operation both need to be passed to the annotation. 12 BUILDING A REST API
Response Object. This is part of us honoring the API contract we mentioned earlier. ResponseEntity - by default Spring returns this object which has some basic data for a response, but not enough for a Great API! 15 BUILDING A REST API
generate release notes and return a JSON response containing them. First, we need to talk to the upstream service and get the commit messages. 16 BUILDING A REST API
we signed a contract! Giving your API user a consistent error handling mechanism is both important and necessary for you to hold up your end of the contract. With good error handling, your API users will no what mistake they made Or what limitations your API currently has… Luckily, Spring gives us some of this out of the box with the ResponseEntityExceptionHandler class 22 BUILDING A REST API
bunch of error handling built in we can override some of its methods to provide our own behavior! 24 BUILDING A REST API Other errors include: NoHandlerFoundException, HttpMessageNotWriteableException, HttpMessageNotReadableException, And…. Many others Wouldn’t it be nice though to add onto this error handling, in our own way?
handling! The controller we wrote earlier included custom request headers. How do we handle those? Create a custom Exception! Add the @ResponseStatus annotation onto a class that extends from Exception 25 BUILDING A REST API
tells the Spring application to use this class in the configuration of the application @EnableSwagger2 - tells Spring to enable the Swagger 2 feature @Bean - object that is instantiated, assembled, and otherwise managed by a Spring IoC container. This is Springs dependency injection mechanism. 30 DOCUMENTATION API INFORMATION Wrapped in an ApiInfo object we can add a Title, description & version to the docs
internet 2. API Contracts - my opinion 3. SpringBoot with Kotlin - https://bit.ly/2XwrOiM 4. Exception Handling - https://bit.ly/37qtH5a 5. Documentation & Testing with Postman - https://swagger.io/ 6. Deployment to Elastic Beanstalk on AWS 7. Wrapping it all up with a CI build that uses the API 34 CONCEPTS