42
Simple REST-APIs with Dropwizard and Swagger Bernd Schönbach LeanIX GmbH

Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Simple REST-APIs withDropwizard and Swagger

Bernd Schönbach

LeanIX GmbH

Page 2: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Motivation

2

• Quickly create REST-APIs

• Make it testable

• Deploy with a click

• Or even better automatically

• Provide Documentation without MS Office

• Provide SDK with no extra action needed

Page 3: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Agenda

3

Introduction• About me• About LeanIX

Dropwizard• Set Up / Basic Features• Demo• Advanced Features

Swagger + Swagger UI• Set Up / Basic Features• Demo• Advanced Features

Page 4: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Agenda

4

Introduction• About me• About LeanIX

Dropwizard• Set Up / Basic Features• Demo• Advanced Features

Swagger + Swagger UI• Set Up / Basic Features• Demo• Advanced Features

Page 5: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

About me

5

EnvironmentBernd Schönbach

• Est.: 1982

• First Language: PHP (2001)

• Second Language: Java (2010)

• Java-Dev. @LeanIX: 2015 -

Page 6: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

IT Architectures are more important than ever before

6

• Shorter time to market

• Real-time big data insights

• Efficient scalability

• Exceptional user experience

• Consistent channels

• Integrated workflows

• …

More• APIs• Devices• Sensors

Page 7: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

LeanIX is the most comfortable solution – as SaaS

7

Available Integrations

Page 8: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Well-known brands of all industries trust in LeanIX

Industrials &Manufacturing

Financial &Insurance Consumer

Energy &Materials Pharma

TravelLogistics

MediaTelecoms

INTERNATIONALEXPANSION

8

Page 9: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

LeanIX is based on a modern Microservices Stack

9

Pathfinder

Po

stgr

es

Ela

stic

sear

ch

Ara

ngo

DB

Pathfinder UISingle Page App•Angular (2)• Typescript

Backend Services•GraphQL or REST API• oAuth2 /w JWT• Swagger API Docs• Event-Bus (Webhooks)

Resources•No sharing across

services•Deployment with

Docker

Web-hooks

MTM Export Images Survey Metrics

Po

stgr

es

Kaf

ka

Po

stgr

es

Shib

bo

leth

Ph

anto

mJS

Po

stgr

es

Ela

stic

sear

ch

Infl

uxD

B

“Platform Services”

Page 10: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Agenda

10

Introduction• About me• About LeanIX

Dropwizard• Set Up / Basic Features• Demo• Advanced Features

Swagger + Swagger UI• Set Up / Basic Features• Demo• Advanced Features

Page 11: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

11

Live  Demo

Overview

What is it?

The Basics

Advanced Features

• Java Framework for RESTful Web Services

• Quick Startup (few seconds)

• Completely configurable

• Current Version: 1.1.2 (we will be using 1.1.1)

Page 12: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

12

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...

Page 13: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

13

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

Page 14: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

14

Add dependency to pom.xmlStandard way (dropwizard only)

What is it?

The Basics

Advanced Features

<properties><dropwizard.version>1.1.2</dropwizard.version>

</properties>

Step 1:

Step 2:

<dependencies><dependency>

<groupId>io.dropwizard</groupId><artifactId>dropwizard-core</artifactId><version>${dropwizard.version}</version>

</dependency></dependencies>

Page 15: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

15

Add dependency to pom.xmlOur way (Swagger & Dropwizard together)

What is it?

The Basics

Advanced Features

<properties><dropwizard.version>1.1.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>

Page 16: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

16

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/froscon-test.jar</outputFile>[...]

</configuration></execution>

</executions> </plugin>

Page 17: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Let’s code!

Page 18: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

18

Configure your Application

What is it?

The Basics

Advanced Features

config.yml

server: applicationConnectors:

type: httpport: 1337

adminConnectors:type: httpport: $env:ADMIN_PORT:1338

Logging:level: INFO

FooBar: Value

Page 19: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

19

Resource Methods and Paths

What is it?

The Basics

Advanced Features

@Path(“persons”)public class PersonResource {

@GETpublic 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

Page 20: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

20

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) {[…]}

Page 21: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

21

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 () {[…]

}

Page 22: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

22

What is it?

The Basics

Advanced Features

Advanced Features

- HATEOAS

- Validators and Defaults for Query Parameters

- Authentication Handling

Page 23: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

23

Pimping your Response (HATEOAS)

- Add Meta Information to Response Data

- Provide Links to allow automated traversal (by machines)

{data: [{…},…,{…}]size: 10total: 150links: {

next: “/persons/page=2&limit=10”,current: “/persons/page=1&limit=10”,previous: null

}}

What is it?

The Basics

Advanced Features

Page 24: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

24

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;

}}

Page 25: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

25

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

Page 26: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

26

Validators and Defaults forQuery Parameters

What is it?

The Basics

Advanced Features

public class PersonResource {

@GETpublic 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

Page 27: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

27

Authentication Handling

What is it?

The Basics

Advanced Features

Step 1: Create Authentication Provider:

public class ExampleAuthenticator implementsAuthenticator<BasicCredentials, User> {@Overridepublic Optional<User> authenticate(BasicCredentials credentials

) throws AuthenticationException{if ("secret".equals(credentials.getPassword())) {return Optional.of(new User(credentials.getUsername()));

}return Optional.absent();

}}

Page 28: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

28

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)

);}

Page 29: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Dropwizard

29

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 )

{[…]

}}

Page 30: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Agenda

30

Introduction• About me• About LeanIX

Dropwizard• Set Up / Basic Features• Demo• Advanced Features

Swagger + Swagger UI• Set Up / Basic Features• Demo• Advanced Features

Page 31: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Swagger

31

Swagger + Swagger UI

What is it?

The Basics

Advanced Features

- Simple documentation for REST APIs

- Allows automatic SDK Generation

- Nice and interactive User Interface

Page 32: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

32

Page 33: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Swagger Workflow

33

JSONSpecification

HTML API UI

SDK Generation

CodeAnnotation

Page 34: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Let’s code!

Page 35: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Swagger

35

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)

Page 36: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Swagger

36

Basic Annotations

What is it?

The Basics

Advanced Features

@ApiParam(name = String,value = String,

defaultValue = String,required = Boolean,

allowableValues = String,example= String,hidden = Boolean

)

Page 37: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Swagger

37

Further Annotations

What is it?

The Basics

Advanced Features

@ApiModel(value= String, description= String

)

@ApiModelProperty(value = String, allowableValues= String

)

@Contact

@License

Page 38: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Swagger

38

Live  Demo

Advanced FeaturesSDK 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

Page 39: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Questions?

Page 40: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Thank you!

Page 41: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Sources

• Dropwizard: http://www.dropwizard.io/

• Swagger: http://swagger.io/

• Swagger-UI: http://swagger.io/swagger-ui/

• Hibernate Validator: http://hibernate.org/validator/

• Dropwizard Swagger Package: https://github.com/smoketurner/dropwizard-swagger

Page 42: Simple REST-APIs with Dropwizardand Swagger...2017/08/18  · Simple REST-APIs with Dropwizardand Swagger Bernd Schönbach LeanIX GmbH Motivation 2 • Quickly create REST-APIs •

Slides will be available at

Slideshare: http://de.slideshare.net/leanIX_net/