203 Lotus blogs updated hourly. Who will post next? Home | Blogs | Search | About 
 
Latest 7 Posts
Building Facebook Messenger Bots with Botkit and Watson
Thu, Nov 16th 2017 38
Creating Alexa Skills with OpenWhisk and IBM Watson
Thu, Nov 16th 2017 34
Integrating Applications without writing Code
Tue, Nov 14th 2017 128
Pictures from W-JAX 2017
Fri, Nov 10th 2017 43
Using regular Expressions for Entities in Conversations
Fri, Nov 3rd 2017 8
My Favorite IBM Developer Journeys
Wed, Oct 18th 2017 5
Fast On-Ramp for building Cloud-Native Apps
Tue, Oct 17th 2017 5
Top 10
Integrating Applications without writing Code
Tue, Nov 14th 2017 128
Pictures from W-JAX 2017
Fri, Nov 10th 2017 43
Building Facebook Messenger Bots with Botkit and Watson
Thu, Nov 16th 2017 38
Creating Alexa Skills with OpenWhisk and IBM Watson
Thu, Nov 16th 2017 34
Deploying Angular 2 Apps to Bluemix
Tue, Apr 11th 2017 27
Getting started with Tensorflow on IBM Bluemix
Mon, Apr 3rd 2017 14
How to build Facebook Chatbots with IBM Watson
Thu, May 12th 2016 13
Running the Sample App BlueChatter on Kubernetes
Mon, Apr 10th 2017 12
New Slots Filling Functionality in Watson Conversation
Fri, Jun 23rd 2017 12
How to write AngularJS Frontends for LoopBack Applications
Mon, Feb 29th 2016 11


Usage of Swagger 2.0 in Spring Boot Applications to document APIs
Twitter Google+ Facebook LinkedIn Addthis Email Gmail Flipboard Reddit Tumblr WhatsApp StumbleUpon Yammer Evernote Delicious
Niklas Heidloff    

As IBM VP Angel Diaz stated in an interview on SearchCloudComputing “Swagger is the way most developers describe [REST] APIs”. With version 2.0 many important features like extensibility have been added, there is a big community and many developers are using it by now. Additionally there is work going on to create an open governance model around the Swagger specification under the Linux Foundation as part of the Open API Initiative. What I like most about Swagger is the ability to document APIs directly in the (Java) source code via annotations so that documentation and actual API implementations are not out of synch.

IBM uses Swagger for the API documentation of several products and services internally. Furthermore the API management service in IBM Bluemix can import Swagger 2.0 definitions to manage your APIs easily.

The Java Spring framework has a lot of traction in the enterprise community these days. In order to use Swagger in Spring Boot applications I tried Springfox which is not part of Swagger core but it integrates nicely in Spring and supports the core Swagger annotations.

Below I extended the Spring sample Building a RESTful Web Service with Swagger annotations.

First you need to define the dependencies to the Springfox and Swagger libraries, in my case in Maven.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>org.springframework</groupId>
	<artifactId>gs-rest-service</artifactId>
	<version>0.1.0</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.0.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.2.2</version>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.2.2</version>
			<scope>compile</scope>
		</dependency>
	</dependencies>

	<properties>
		<java.version>1.8</java.version>
	</properties>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-releases</id>
			<url>https://repo.spring.io/libs-release</url>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>spring-releases</id>
			<url>https://repo.spring.io/libs-release</url>
		</pluginRepository>
	</pluginRepositories>
</project>

Then I enabled Swagger in the boot application.

package hello;

import static springfox.documentation.builders.PathSelectors.regex;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import com.google.common.base.Predicate;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
@ComponentScan("hello")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    @Bean
    public Docket newsApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("greetings")
                .apiInfo(apiInfo())
                .select()
                .paths(regex("/greeting.*"))
                .build();
    }
    
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring REST Sample with Swagger")
                .description("Spring REST Sample with Swagger")
                .termsOfServiceUrl("http://www-03.ibm.com/software/sla/sladb.nsf/sla/bm?Open")
                .contact("Niklas Heidloff")
                .license("Apache License Version 2.0")
                .licenseUrl("https://github.com/IBM-Bluemix/news-aggregator/blob/master/LICENSE")
                .version("2.0")
                .build();
    }
}

In the controller I used Swagger annotations to document the API.

package hello;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.ResponseHeader;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @ApiOperation(value = "getGreeting", nickname = "getGreeting")
    @RequestMapping(method = RequestMethod.GET, path="/greeting", produces = "application/json")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "User's name", required = false, dataType = "string", paramType = "query", defaultValue="Niklas")
      })
    @ApiResponses(value = { 
    		@ApiResponse(code = 200, message = "Success", response = Greeting.class),
    		@ApiResponse(code = 401, message = "Unauthorized"),
    		@ApiResponse(code = 403, message = "Forbidden"),
    		@ApiResponse(code = 404, message = "Not Found"),
    		@ApiResponse(code = 500, message = "Failure")})	
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
        		String.format(template, name));
    }
}

In the sample resource I documented parameters.

package hello;

import com.fasterxml.jackson.annotation.JsonProperty;

import io.swagger.annotations.ApiModelProperty;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    @JsonProperty(required = true)
    @ApiModelProperty(notes = "The name of the user", required = true)
    public String getContent() {
        return content;
    }
}

Here is the outcome in the API explorer.

swaggerspring

The post Usage of Swagger 2.0 in Spring Boot Applications to document APIs appeared first on Niklas Heidloff.



---------------------
http://heidloff.net/article/usage-of-swagger-2-0-in-spring-boot-applications-to-document-apis/
Nov 17, 2015
8 hits



Recent Blog Posts
38
Building Facebook Messenger Bots with Botkit and Watson
Thu, Nov 16th 2017 10:54a   Niklas Heidloff
I’ve implemented a sample Facebook Messenger bot which leverages the Node.js library Botkit and IBM Watson Conversation. With Botkit you can build bots for Slack, Facebook Messenger, Twilio and other messaging clients. For natural language understanding different services can be used, for example IBM Watson, Api.ai, Wit.ai or Rasa. Most of the code can be shared for Slack and Facebook Messenger bots. Check out the code on GitHub. Similarly to my sample Slack bot, the Facebook Messenger bo
34
Creating Alexa Skills with OpenWhisk and IBM Watson
Thu, Nov 16th 2017 8:43a   Niklas Heidloff
My first code pattern (formerly known as developer journeys) has been published on developer.ibm.com/code. The pattern describes how to build Alexa skills with OpenWhisk. Rather than using intent and entity identification from Alexa, Watson Conversation is used. This allows developers to define conversation flows declaratively and developers can reuse conversations that are available in the Bot Asset Exchange. My colleague Mark Sturdevant improved the documentation and created a video. Check out
128
Integrating Applications without writing Code
Tue, Nov 14th 2017 2:52p   Niklas Heidloff
IBM App Connect is an IPaaS (Integration Platform as a Service) to connect different applications without having to write code. Similarly to IFTTT users are typically business users or citizen developers from line of business departments. App Connect provides currently roughly 50 connectors to integrate applications like Salesforce, SAP, Slack and Gmail. See the documentation for details. Users can graphically build flows that are either event-triggered, for example if a document is changed in a
43
Pictures from W-JAX 2017
Fri, Nov 10th 2017 10:32a   Niklas Heidloff
This week I attended W-JAX in Munich, Germany, which is a developer conference with 1400 attendees with a focus on Java and enterprise technologies. My colleagues and I presented and demonstrated the IBM Cloud and the IBM Code developer site. Below are some pictures. The IBM team: Discussions with developers at the booth: I gave a session about Slack bots and IBM Watson Conversation: One thing I really liked was a demo another company gave at their booth. They connected Anki Overdrive cars to
8
Using regular Expressions for Entities in Conversations
Fri, Nov 3rd 2017 8:06a   Niklas Heidloff
In conversational experiences entities are used to identify classes of objects in user phrases, for example ‘city’ or ‘person’. Watson Conversation supports entity definitions with predefined lists of potential values and synonyms. Recently a new capability has been added so that you can also use regular expressions to identify entities (beta). This functionality is useful when, for example, you want to identify email addresses, phone numbers or URLs without predefining a
5
My Favorite IBM Developer Journeys
Wed, Oct 18th 2017 3:12p   Niklas Heidloff
The team of developer advocates within IBM has created a new site with journeys for developers. Journeys are sample scenarios that are available as open source, well documented and easy to reuse and deploy. There are roughly 60 journeys right now and new ones are published regularly. Check out the journeys at developer.ibm.com/code/journey. Here are my favorite journeys: Make Java microservices resilient with Istio “Twelve-factor apps make a strong case for designing and implementing you
5
Fast On-Ramp for building Cloud-Native Apps
Tue, Oct 17th 2017 9:32a   Niklas Heidloff
In order for developers to get started quickly to build applications and deploy them on the IBM Cloud, new starter kits have been made available. Check out the new IBM Cloud App Service. You can choose from several template the type of application or microservice you want to build: Express.js Backend, Express.js React, Java MicroProfile, Java Spring Backend, Swift Kitura, Python Microservice and more. Additionally you can create new or select existing IBM Cloud services, for example the Watson s
3
How to write your own Slack Chatbots in Javascript
Mon, Oct 16th 2017 2:54p   Niklas Heidloff
Last week Ansgar Schmidt and I gave a session at Codemotion How to write your own Slack Chatbots in Javascript. Below are the slides with screenshots of our demos. We demonstrated how to write a Node.js application that uses Botkit to connect with Slack and that uses IBM Watson Conversation as natural language processing service. Get the code of our sample from GitHub. The post How to write your own Slack Chatbots in Javascript appeared first on Niklas Heidloff.
6
Composing Serverless Apps with IBM Cloud Functions
Tue, Oct 10th 2017 8:13p   Niklas Heidloff
Today at Serverlessconf IBM announced a new key capability (as IBM Research preview) of IBM Cloud Functions. With the new tool ‘Composer‘ apps can be created which contain multiple cloud functions. These apps coordinate the invocations of actions and the data flow. Compared to the previously available action sequences the new functionality is much more flexible. Cloud functions are typically rather simple and focussed on specific tasks which is why people often refer to cloud functio
4
Testing Watson Conversations in Slack
Thu, Oct 5th 2017 7:03a   Niklas Heidloff
Next week Ansgar Schmidt and I will give a session ‘How to write your own Slack Chatbots in Javascript‘ at codemotion. We will demonstrate how to connect Slack to Watson Conversation via the open source project botkit which leverages the Slack Real Time Messaging API. This works well for production bots, but requires some configuration first. For testing purposes there is an easier way to create a Slack app with a bot user and to add this to a Slack team. From the ‘Deploy’




Created and Maintained by Yancy Lent - About - Planet Lotus Blog - Advertising - Mobile Edition