Skip to content

Instantly share code, notes, and snippets.

View biniama's full-sized avatar

Biniam biniama

View GitHub Profile
@biniama
biniama / DateFormatFinder.java
Last active November 19, 2019 15:31
Find date format given the date object
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Source: https://stackoverflow.com/questions/7579227/how-to-get-the-given-date-string-formatpattern-in-java
*/
public class DateFormatFinder {
@biniama
biniama / SwaggerDocConfig.java
Last active March 12, 2020 15:28
Swagger Documentation with Spring Boot (working UI)
package com.example.springbootswagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
@biniama
biniama / PojoToJson.java
Created November 2, 2018 13:50
Convert POJO to JSON
import com.fasterxml.jackson.databind.ObjectMapper;
private static void pojoToJson(Object object) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
log.info(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object));
}
@biniama
biniama / How to run Simple Server Locally
Last active October 30, 2018 09:54
Simple API Server using Spring Boot
1. Make sure Spring Boot is installed
`sdk install springboot`
`spring --version`
2. Run
`spring run app.groovy`
@biniama
biniama / Difference
Last active October 20, 2018 14:25
Adapter vs Decorator vs Bridge
In scope and intent
Adapter - intent is to mix an interface into a class that doesn't currently implement htat interface.
Decorator - intent is to change the way a method behaves differently for different decorators.
Bridge - intent is to insolate sub-systems so you can swap out sub-systems without impacting the code that is using that sub-systems.
The point of a Bridge is to isolate two subsystems so that they can be independently modified. Bridges are big things, often comprised of other patterns (like Adapters and Decorators). This segment looks at how to implement a Bridge and discusses the differences between Bridge and similar patterns like Adapter.
E.g. JDBC (bad implementation of Bridge since swapping doesn't work as it should)
Best example: ORM systems like Hibernate.
@biniama
biniama / SingeltonGoF.java
Last active October 20, 2018 12:07
Singleton Pattern - Multiple Implementations by Allen Holub (holub.com/patterns)
class Singelton {
static Singleton instance = null;
//private constructor
private Singelton() {}
public static Singleton getInstance() {
if(instance == null)
instance = new Singleton();
return instance;
@biniama
biniama / PhonePadExercise.java
Last active October 17, 2018 07:44
"Find Unique Numbers of Length n" using Java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PhonePadExercise {
public static void main(String[] args) {
Integer selectedDigit = 4;
@biniama
biniama / Array.java
Created May 24, 2018 23:07
Array Example
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
/*
Define an array and insert element to an array
public class HelloWorld {
public static void main(String [] args) {
System.out.println("Hello, Ese and Haile!");
}
}
//public - accessible from anywhere
//static - only a single instance while executing
//void - does not return anything
//Function(method) = input -> processing -> output

Swagger Setup for Embedded Jetty Server

In setting up a Jetty server with Jersey servlets, you may choose to use an embedded Jetty server setup. (See here for how to setup an embedded Jetty server). In this gist, we'll go through how to setup Swagger for this setup. I am using Swagger 1.5, Maven 3.3.3, Jersey 1.8, and Jetty 7.3. Make sure you add all dependencies to your pom.xml.

In the Swagger Core setup, the current official recommendations involve an Application class, or a web.xml, neither of which are used in an embedded Jetty server setup. To add Swagger to your embedded Jetty Server, you must do 3 things:

  1. Add the package scanning packages to your servlets which will serve your REST API.
  2. Add the Swagger package scanning servlet.
  3. P