During the VOXXED Days in Berlin on 29 January 2016 Bernd Schönbach from LeanIX demonstrated an easy way to create well documented and implemented REST-APIs using the Dropwizard Library for the implementation and Swagger for easy Documentation.
===
LeanIX offers an innovative software-as-a-service solution for Enterprise Architecture Management (EAM), based either in a public cloud or the client’s data center.
Companies like Adidas, Axel Springer, Helvetia, RWE, Trusted Shops and Zalando use LeanIX Enterprise Architecture Management tool.
Free Trial: http://bit.ly/LeanIXFreeTrial
2. Motivation
2REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
- Quickly create REST-APIs
- Make them testable
- Make them deployable with a click
- Or even better automatically
- Provide Documentation without MS Office
- Provide SDK with no extra action needed
3. Overview
3REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Introduction
• About me
• About LeanIX
Dropwizard
• Set Up / Basic Features
• Demo
• Advanced Features
Swagger + Swagger UI
• Set Up / Basic Features
• Demo
• Advanced Features
5. About me
5REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
EnvironmentBernd Schönbach
• Est.: 1982
• First Language: PHP (2001)
• Second Language: Java (2010)
• Java-Dev. @leanIX: 2015 -
6. LeanIX was born, since pragmatic solutions for
IT Architecture Management did not exist
6REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Jörg G. Beyer André Christ
Management-Team & FounderLeanIX GmbH
• Headquarter: Bonn
• Founded: 01/2012
• Launch: 08/2012
• 4 years Management
Consulting @ DHL
• 8 years Freelancer
Software Design &
Architecture
• Speaker & Author for
Software Architecture
• 10 years IT Leadership @
DHL, e.g. global CIO
• 4 years MD for newly
established IT Management
Consulting
• 10 years Consulting:
Andersen & self-employed
7. LeanIX helps companies to manage and
optimize their IT Architectures
7REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Current IT Architecture Create Transparency Optimize IT Architecture
• Missing information (e.g.
interfaces, technologies)
• Hard to introduce new
products & sales channels
• High costs and risks
• Import existing data into
LeanIX (via Excel or API)
• Invite experts to share
their knowledge
• Use best-practice reports
to identify issues
• Define target architecture
and roadmaps
Learn more: https://www.leanix.net/en/product/index
8. LeanIX is a web-based platform
to capture and share knowledge about IT
8REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Fact Sheets & Tagging
Context-based Search
API, Import & Export
Comments & Threads
IT Inventory Collaboration Platform Interactive Reporting
Activity Stream &
Notifications
Subscriptions
Print & Export (PDF)
Best Practice Reports
Interactive Adaption
Learn more: https://www.leanix.net/en/product/index
9. Market leading companies already use LeanIX
9
Media
Insurance
Industry
e-Commerce
Telecoms
Chemical
Advisory
Government
Manufacturing
Logistics
Education
Health
Energy
See more: https://www.leanix.net/en/customers/index
11. Dropwizard
11REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Live Demo
Overview
What is it?
The Basics
Advanced Features
• Java Framework for RESTful Web Services
• Quick Startup (few seconds)
• Completely configurable
• Current Version: 0.9.2
(we will be using 0.9.1)
12. Dropwizard
12REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Live Demo
Overview
What is it?
The Basics
Advanced Features
• Framework Contents:
• Jetty for HTTP
• Jersey for REST
• Jackson for JSON transformation
• Hibernate for DB Access
• Other usefull inclusions
• Metrics
• Liquibase
• Slf4j and Logback
• and much more...
13. Dropwizard
13REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
First Steps:
What is it?
The Basics
Advanced Features
• Create Maven Project
• Add dependency to dropwizard
• Configure Maven Shade Plugin
• Add configuration file (optional)
• Start coding
14. Dropwizard
14REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Add dependency to pom.xml
Standard way (dropwizard only)
What is it?
The Basics
Advanced Features
<properties>
<dropwizard.version>0.9.1</dropwizard.version>
</properties>
Step 1:
Step 2:
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
</dependencies>
15. Dropwizard
15REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Add dependency to pom.xml
Our way (Swagger & Dropwizard together)
What is it?
The Basics
Advanced Features
<properties>
<dropwizard.version>0.9.1-1</dropwizard.version>
</properties>
Step 1:
Step 2:
<dependencies>
<dependency>
<groupId>com.smoketurner</groupId>
<artifactId>dropwizard-swagger</artifactId>
<version>${dropwizard.version} </version>
</dependency>
</dependencies>
16. Dropwizard
16REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Configure Maven Shade Plugin
What is it?
The Basics
Advanced Features
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration combine.children="append">
<createDependencyReducedPom>
true
</createDependencyReducedPom>
<outputFile>target/voxxed-test.jar</outputFile>
[...]
</configuration>
</execution>
</executions>
</plugin>
18. Dropwizard
18REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Configure your Application
What is it?
The Basics
Advanced Features
config.yml
server:
applicationConnectors:
type: http
port: 1337
adminConnectors:
type: http
port: $env:ADMIN_PORT:1338
Logging:
level: INFO
FooBar: Value
19. Dropwizard
19REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Resource Methods and Paths
What is it?
The Basics
Advanced Features
@Path(“persons”)
public class PersonResource {
@GET
public String getPersons () {[…]}
@PUT
public String updatePerson() {[…]}
@POST
public String createPerson() {[…]}
@DELETE
public String deletePerson() {[…]}
URL: localhost:1337/persons
@GET
@Path("/address"}
public String getPersonAddress() {[…]}
URL: localhost:1337/persons/address
20. Dropwizard
20REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Query and Path Params
What is it?
The Basics
Advanced Features
Task 1: Search an Entity.
URL: http://localhost:1337/persons?name=adam
Task 2: Retrieve an Entity:
URL: http://localhost:1337/persons/1234/
Query Parameters
public class PersonResource {
@GET
public String getPerson(
@QueryParam(“name”) String name
) {[…]}
Path Parameters
public class PersonResource {
@GET
@Path(“/{id}”)
public String getPersonById(
@PathParam(“id”) String id
) {[…]}
21. Dropwizard
21REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Response Types
What is it?
The Basics
Advanced Features
Return and retrieve JSON
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class PersonResource {
@GET
public String getPerson (
@QueryParam(“name”) String name
) {[…]}
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class PersonResource {
@GET
public String getPerson (
@QueryParam(“name”) String name
) {[…]}
@GET
@Path(“xml”)
@Produces(MediaType.APPLICATION_XML)
public String getPersonXml (){
[…]
}
22. Dropwizard
22REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
What is it?
The Basics
Advanced Features
Advanced Features
- HATEOAS
- Validators and Defaults for Query Parameters
- Authentication Handling
23. Dropwizard
23REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Pimping your Response (HATEOAS)
- Add Meta Information to Response Data
- Provide Links to allow automated traversal (by machines)
{
data: [{…},…,{…}]
size: 10
total: 150
links: {
next: “/persons/page=2&limit=10”,
current: “/persons/page=1&limit=10”,
previous: null
}
}
What is it?
The Basics
Advanced Features
24. Dropwizard
24REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Pimping your Response (HATEOAS)
Response Objects
What is it?
The Basics
Advanced Features
public class BasicResponse {
protected ResponseStatus status = ResponseStatus.OK;
protected String type;
protected String message;
protected Long total;
[…]
}
public class PersonResponse extends BasicResponse {
protected Person data;
protected Links links;
public class Links {
protected String next;
protected String previous;
protected String current;
}
}
25. Dropwizard
25REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Pimping your Response (HATEOAS)
Option B: Jersey Links
What is it?
The Basics
Advanced Features
public void run(AppConfig conf, Environment env) throws Ex {
environment.jersey().getResourceConfig()
.packages(getClass().getPackage().getName())
.register(DeclarativeLinkingFeature.class);
}
App.java
- Add dependency to jersey-server-linking
- Register in run function of App.java
- Add @Link or @Ref Annotations
For further Information see:
https://jersey.java.net/documentation/1.19/linking.html
26. Dropwizard
26REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Validators and Defaults for
Query Parameters
What is it?
The Basics
Advanced Features
public class PersonResource {
@GET
public String getPerson (
@QueryParam(“name”) String name
) {[…]}
public class PersonResource {
@GET
public String getPerson (
@QueryParam(“name”) @NotEmpty String name
) {[…]}
Validating:
Supplied by Hibernate Validator
@NotNull
@Min(Integer); @Max(Integer)
@Size(min = Integer, max = Integer)
@Future, @Past
@Pattern(regex= String)
@Valid
27. Dropwizard
27REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Authentication Handling
What is it?
The Basics
Advanced Features
Step 1: Create Authentication Provider:
public class ExampleAuthenticator implements
Authenticator<BasicCredentials, User> {
@Override
public Optional<User> authenticate(
BasicCredentials credentials
) throws AuthenticationException
{
if ("secret".equals(credentials.getPassword())) {
return Optional.of(new User(credentials.getUsername()));
}
return Optional.absent();
}
}
28. Dropwizard
28REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Authentication Handling
What is it?
The Basics
Advanced Features
Step 2: Register Authentication Provider:
public void run(AppConfig conf, Environment env) throws Ex {
environment.jersey().register(
new AuthDynamicFeature(
new BasicCredentialAuthFilter.Builder<User>()
.setAuthenticator(new ExampleAuthenticator())
.setRealm("SUPER SECRET STUFF") .buildAuthFilter()
)
);
environment.jersey().register(
RolesAllowedDynamicFeature.class
);
environment.jersey().register(
new AuthValueFactoryProvider.Binder<>(User.class)
);
}
29. Dropwizard
29REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Authentication Handling
What is it?
The Basics
Advanced Features
Step 3: Use Auth Parameter:
public class PersonResource {
@GET
public String getPerson (
@QueryParam(“name”) @NotEmpty String name,
@Auth User auth )
{
[…]
}
}
31. Swagger
31REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Swagger + Swagger UI
What is it?
The Basics
Advanced Features
- Simple documentation for REST APIs
- Allows automatic SDK Generation
- Nice and interactive User Interface
35. Swagger
35REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Basic Annotations
What is it?
The Basics
Advanced Features
@Api(“Description”)
@ApiResponse
@ApiResponses
@ApiOperation(
value = String,
notes = String,
response = Class,
responseContainer = String
)
@ApiParam()
@DefaultValue(mixed)
36. Swagger
36REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Basic Annotations
What is it?
The Basics
Advanced Features
@ApiParam(
name = String,
value = String,
defaultValue = String,
required = Boolean,
allowableValues = String,
example= String,
hidden = Boolean
)
37. Swagger
37REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Further Annotations
What is it?
The Basics
Advanced Features
@ApiModel(
value= String, description= String
)
@ApiModelProperty(
value = String, allowableValues= String
)
@Contact
@License
38. Swagger
38REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
Live Demo
Advanced Features
SDK Generation
What is it?
The Basics
Advanced Features
Example for PHP and Java:
https://github.com/leanix/swagger-demo-codegen
Configuration for that:
https://goo.gl/plH6pB
39. Thank you!
Learn More about LeanIX
Share on LinkedIn
http://bit.ly/LeanIXDemoS
Request free demo
http://bit.ly/LeanIXDemoS
42. Slides will be available at
REST-APIs with Dropwizard & Swagger – VoxxedDays Berlin 2016 – Bernd Schönbach – LeanIX GmbH
LeanIX Website: https://www.leanix.net/
LeanIX Blog: http://blog.leanix.net/
Slideshare: http://slideshare.net/leanIX_net/
Share on LinkedIn