Skip to content

Instantly share code, notes, and snippets.

View javaeeeee's full-sized avatar

Dmitry Noranovich javaeeeee

View GitHub Profile
@javaeeeee
javaeeeee / dropwizard archetype
Last active August 29, 2015 14:14
Create a Dropwizard project using Maven archetype
mvn archetype:generate
-DgroupId=com.javaeeeee
-DartifactId=DWGettingStarted
-Dname=DWGettingStarted
-Dpackage=com.javaeeeee.dwstart
-DarchetypeGroupId=io.dropwizard.archetypes
-DarchetypeArtifactId=java-simple
-DinteractiveMode=false
@javaeeeee
javaeeeee / dropwizard resource
Created January 24, 2015 08:42
A simple Dropwizard/Jersey Resource Class
@Path("/hello")
public class HelloResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getGreeting() {
return "Hello world!";
}
}
@javaeeeee
javaeeeee / dropwizard register resource
Created January 24, 2015 08:49
Dropwizard: registering a resource
public void run(final DWGettingStartedConfiguration configuration,
final Environment environment) {
environment.jersey().register(new HelloResource());
}
@javaeeeee
javaeeeee / dropwizard testing
Created January 24, 2015 08:51
Add necessary dependencies to test Dropwizard
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-testing</artifactId>
<version>${dropwizard.version}</version>
<scope>test</scope>
</dependency>
@javaeeeee
javaeeeee / test resource dropwizard
Created January 24, 2015 08:52
A jUnit test for Dropwizard/Jersey Resource
public class HelloResourceTest {
@Rule
public ResourceTestRule resource = ResourceTestRule.builder()
.addResource(new HelloResource()).build();
@Test
public void testGetGreeting() {
String expected = "Hello world!";
//Obtain client from @Rule.
@javaeeeee
javaeeeee / chain methods
Created January 24, 2015 08:53
Jersey client method chaining
actual = resource.client()
.target("http://localhost:8080/hello")
.request(MediaType.TEXT_PLAIN)
.get(String.class);
assertEquals(expected, actual);
@javaeeeee
javaeeeee / path param sub-resource
Created January 24, 2015 08:55
Dropwizard/Jersey sub-resource method using path parameters
@Path("/path_param/{name}")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getNamedGreeting(@PathParam(value = "name") String name) {
return "Hello " + name;
}
@javaeeeee
javaeeeee / query param sub-resource
Created January 24, 2015 08:56
Dropwizard/Jersey sub-resource method using query parameters
@Path("/query_param")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getNamedStringWithParam(@DefaultValue("world") @QueryParam("name") String name) {
return "Hello " + name;
}
@javaeeeee
javaeeeee / dropwizard representation
Created January 24, 2015 08:58
A simple Dropwizard representation class
public class Greeting {
@JsonProperty
private String greeting;
public Greeting() {
}
public Greeting(String greeting) {
this.greeting = greeting;
@javaeeeee
javaeeeee / dropwizard JSON subresource
Created January 24, 2015 08:59
A simple Dropwizard/Jersey sub-resource serving JSON
@Path("/hello_json")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Greeting getJSONGreeting() {
return new Greeting("Hello world!");
}