Skip to content

Instantly share code, notes, and snippets.

@emoseman
Created August 27, 2012 18:32
Show Gist options
  • Save emoseman/3491166 to your computer and use it in GitHub Desktop.
Save emoseman/3491166 to your computer and use it in GitHub Desktop.
Sample Java Jersey REST Resource
package your.org.here;
/**
* Class Description todo
*
* @author Your Name ([email protected])
* @since 7/16/12
* @version 1.0
*/
import javax.naming.NamingException;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.sql.SQLException;
import java.util.logging.Logger;
@Path("/path")
public class CountResource extends BaseResource
{
/* class logger */
private static Logger log = Logger.getLogger("CountResource");
@GET
@Path("{value: [a-zA-Z]+}")
@Produces(MediaType.APPLICATION_JSON)
public Response getJSON(
@PathParam("type") String arg,
@Context UriInfo uriInfo)
{
if (arg == null || arg.isEmpty())
return Response.status(Response.Status.BAD_REQUEST).build();
String result;
try {
result = getValue(arg);
}
catch (Exception e) {
throw new WebApplicationException(e);
}
return Response.ok(gson.toJson(result)).build();
}
@GET
@Path("{type: [a-zA-Z]+}")
@Produces({MediaType.TEXT_PLAIN, MediaType.TEXT_HTML})
public Response getText(@PathParam("type") String type)
{
if (type == null || type.isEmpty())
return Response.status(Response.Status.BAD_REQUEST).build();
String result;
try {
result = getCount(type);
}
catch (Exception e) {
throw new WebApplicationException(e);
}
return Response.ok(result).build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment