Skip to content

Instantly share code, notes, and snippets.

@xpmatteo
Last active May 19, 2017 06:33
Show Gist options
  • Save xpmatteo/681abd463b2f01fa61a76fc48a7fc319 to your computer and use it in GitHub Desktop.
Save xpmatteo/681abd463b2f01fa61a76fc48a7fc319 to your computer and use it in GitHub Desktop.

Sample Java no-frameworks-other-than-servlets microservice.

Run it with

./gradlew build
java -cp build/libs/yourproject-all.jar com.thoughtworks.eg.AnyMicroService 8080
package com.thoughtworks.eg;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
public class AnyMicroService extends HttpServlet {
private static Server server;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (request.getRequestURI().startsWith("/hello")) {
response.setStatus(200);
response.setContentType("application/json");
JsonObject json = new JsonObject();
json.add("hello", new JsonPrimitive("world!"));
response.getWriter().write(json.toString());
return;
}
response.setStatus(404);
}
public static void main(String[] args) throws Exception {
server = new Server(Integer.parseInt(args[0]));
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.addServlet(new ServletHolder(new AnyMicroService()),"/*");
server.setHandler(context);
server.start();
}
public static void stop() throws Exception {
server.stop();
server.join();
}
}
package com.thoughtworks.eg;
import static java.util.stream.Collectors.joining;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertThat;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class AnyMicroServiceTest {
@Before
public void setUp() throws Exception {
AnyMicroService.main(new String[] {"9292"});
}
@After
public void tearDown() throws Exception {
AnyMicroService.stop();
}
@Test
public void hello() throws Exception {
URL url = new URL("http://127.0.0.1:9292/hello");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String result = new BufferedReader(new InputStreamReader(connection.getInputStream()))
.lines().collect(joining("\n"));
assertThat(connection.getResponseCode(), is(200));
assertThat(connection.getContentType(), startsWith("application/json"));
assertThat(result, is("{\"hello\":\"world!\"}"));
}
@Test(expected=FileNotFoundException.class)
public void notFound() throws Exception {
URL url = new URL("http://127.0.0.1:9292/something-else");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.getInputStream();
}
}
apply plugin: 'java'
apply plugin: 'war'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "com.google.code.gson:gson:2.3.1"
compile 'org.eclipse.jetty:jetty-server:9.4.3.v20170317'
compile 'org.eclipse.jetty:jetty-webapp:9.4.3.v20170317'
providedCompile 'javax.servlet:servlet-api:2.5'
testCompile "junit:junit:4.12"
}
task fatJar(type: Jar) {
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
artifacts {
archives fatJar
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment