Skip to content

Instantly share code, notes, and snippets.

@john-nash-rs
Created July 19, 2018 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save john-nash-rs/fcdccfb15ce5cfcebfecd7fbdffaea3c to your computer and use it in GitHub Desktop.
Save john-nash-rs/fcdccfb15ce5cfcebfecd7fbdffaea3c to your computer and use it in GitHub Desktop.
import com.codahale.metrics.annotation.Timed;
import freemarker.template.Template;
import io.dropwizard.hibernate.UnitOfWork;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by harshvardhan on 12/07/18.
*/
@Path("/resource")
public class MyResource {
private InfoDao infoDao;
public MyResource(InfoDao infoDao) {
this.infoDao = infoDao;
}
@GET
@Timed
@Produces({MediaType.APPLICATION_JSON})
@Path("/getName")
public String getName() {
return "Harsh";
}
@POST
@Timed
@Produces({MediaType.APPLICATION_JSON})
@Path("/postName")
public String postName(String name) {
System.out.println("Name given by : "+name);
return "Ok";
}
@GET
@Timed
@Produces({MediaType.APPLICATION_JSON})
@UnitOfWork
@Path("/findAllEmp")
public List<Info> findAllEmp() {
return infoDao.findAll();
}
@POST
@Timed
@UnitOfWork
@Produces({MediaType.APPLICATION_JSON})
@Path("/saveEmp")
public String saveEmp(Info info) {
return infoDao.create(info);
}
@GET
@Timed
@Produces({MediaType.TEXT_HTML})
@Path("/helloWorld")
public Response getHelloWorld() {
try {
Template temp = TemplateConfigurationContext.getConfiguration().getTemplate("helloWorld.ftl");
Map root = new HashMap();
root.put("user", "Harsh");
Writer writer = new StringWriter();
temp.process(root, writer);
return Response.status(Response.Status.ACCEPTED).entity((writer.toString())).build();
} catch (Exception e) {
e.printStackTrace();
}
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(("Oops! Try again later")).build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment