This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mvn archetype:generate | |
-DgroupId=com.javaeeeee | |
-DartifactId=DWGettingStarted | |
-Dname=DWGettingStarted | |
-Dpackage=com.javaeeeee.dwstart | |
-DarchetypeGroupId=io.dropwizard.archetypes | |
-DarchetypeArtifactId=java-simple | |
-DinteractiveMode=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Path("/hello") | |
public class HelloResource { | |
@GET | |
@Produces(MediaType.TEXT_PLAIN) | |
public String getGreeting() { | |
return "Hello world!"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void run(final DWGettingStartedConfiguration configuration, | |
final Environment environment) { | |
environment.jersey().register(new HelloResource()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>io.dropwizard</groupId> | |
<artifactId>dropwizard-testing</artifactId> | |
<version>${dropwizard.version}</version> | |
<scope>test</scope> | |
</dependency> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
actual = resource.client() | |
.target("http://localhost:8080/hello") | |
.request(MediaType.TEXT_PLAIN) | |
.get(String.class); | |
assertEquals(expected, actual); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Path("/path_param/{name}") | |
@GET | |
@Produces(MediaType.TEXT_PLAIN) | |
public String getNamedGreeting(@PathParam(value = "name") String name) { | |
return "Hello " + name; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Path("/query_param") | |
@GET | |
@Produces(MediaType.TEXT_PLAIN) | |
public String getNamedStringWithParam(@DefaultValue("world") @QueryParam("name") String name) { | |
return "Hello " + name; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Greeting { | |
@JsonProperty | |
private String greeting; | |
public Greeting() { | |
} | |
public Greeting(String greeting) { | |
this.greeting = greeting; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Path("/hello_json") | |
@GET | |
@Produces(MediaType.APPLICATION_JSON) | |
public Greeting getJSONGreeting() { | |
return new Greeting("Hello world!"); | |
} |