Created
January 16, 2016 07:13
-
-
Save johnllao/b4e865f0d587e9cb2e2d to your computer and use it in GitHub Desktop.
Jersey REST Sample
This file contains hidden or 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
package org.hello.web.api; | |
import javax.ws.rs.*; | |
import javax.ws.rs.core.MediaType; | |
import org.hello.web.models.Result; | |
@Path("/help") | |
public class HelpApi { | |
@GET | |
@Path("/version") | |
@Produces(MediaType.TEXT_PLAIN) | |
public String version() { | |
return "1.0.0"; | |
} | |
@GET | |
@Path("/status") | |
@Produces(MediaType.APPLICATION_JSON) | |
public Result status() { | |
return new Result("OK", "", "Running"); | |
} | |
} |
This file contains hidden or 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
package org.hello.web; | |
import javax.ws.rs.ext.ContextResolver; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
public class JsonContextResolver implements ContextResolver<ObjectMapper> { | |
final ObjectMapper defaultObjectMapper; | |
public JsonContextResolver() { | |
defaultObjectMapper = createDefaultMapper(); | |
} | |
public ObjectMapper getContext(Class<?> type) { | |
return defaultObjectMapper; | |
} | |
private static ObjectMapper createDefaultMapper() { | |
final ObjectMapper result = new ObjectMapper(); | |
return result; | |
} | |
} |
This file contains hidden or 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
<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/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>org.hello.web</groupId> | |
<artifactId>helloweb</artifactId> | |
<packaging>war</packaging> | |
<version>0.0.1-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>3.8.1</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.glassfish.jersey.containers</groupId> | |
<!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" --> | |
<artifactId>jersey-container-servlet</artifactId> | |
<version>2.22.1</version> | |
</dependency> | |
<!-- Required only when you are using JAX-RS Client --> | |
<dependency> | |
<groupId>org.glassfish.jersey.core</groupId> | |
<artifactId>jersey-server</artifactId> | |
<version>2.22.1</version> | |
</dependency> | |
<dependency> | |
<groupId>org.glassfish.jersey.media</groupId> | |
<artifactId>jersey-media-json-jackson</artifactId> | |
<version>2.22.1</version> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains hidden or 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
package org.hello.web.models; | |
public class Result { | |
private String status; | |
private String data; | |
private String message; | |
public Result(final String status, final String data, final String message) { | |
this.status = status; | |
this.data = data; | |
this.message = message; | |
} | |
public String getStatus() { | |
return status; | |
} | |
public String getData() { | |
return data; | |
} | |
public String getMessage() { | |
return message; | |
} | |
} |
This file contains hidden or 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
<!DOCTYPE web-app PUBLIC | |
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" | |
"http://java.sun.com/dtd/web-app_2_3.dtd" > | |
<web-app> | |
<display-name>Archetype Created Web Application</display-name> | |
<servlet> | |
<servlet-name>api-serlvet</servlet-name> | |
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> | |
<init-param> | |
<param-name>jersey.config.server.provider.packages</param-name> | |
<param-value>org.hello.web.api</param-value> | |
</init-param> | |
<load-on-startup>1</load-on-startup> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>api-serlvet</servlet-name> | |
<url-pattern>/api/*</url-pattern> | |
</servlet-mapping> | |
</web-app> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment