Created
July 19, 2018 09:59
-
-
Save john-nash-rs/fcdccfb15ce5cfcebfecd7fbdffaea3c to your computer and use it in GitHub Desktop.
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
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