Skip to content

Instantly share code, notes, and snippets.

@AnjaliManhas
Created May 10, 2020 11:37
Show Gist options
  • Save AnjaliManhas/721097881b65fb6217f9ac272cfd04c1 to your computer and use it in GitHub Desktop.
Save AnjaliManhas/721097881b65fb6217f9ac272cfd04c1 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.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by AnjaliManhas on 09/05/20.
*/
@Path("/resource")
@Produces(MediaType.APPLICATION_JSON)
public class MyResource {
private InfoDao infoDao;
public MyResource(InfoDao infoDao) {
this.infoDao = infoDao;
}
@GET
@Timed
@Path("/getName")
public String getName() {
return "Anjali";
}
@POST
@Timed
@Path("/postName")
public String postName(String name) {
System.out.println("Name given by : " + name);
return "Ok";
}
@GET
@Timed
@UnitOfWork
@Path("/findAllEmp")
public List<Info> findAllEmp() {
System.out.println("All Emp : " + infoDao.findAll());
return infoDao.findAll();
}
@POST
@Timed
@UnitOfWork
@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", "Anjali");
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