diff --git a/jersey/src/main/java/com/baeldung/jersey/server/Responder.java b/jersey/src/main/java/com/baeldung/jersey/server/Responder.java new file mode 100644 index 0000000000..cb0c976fe3 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/server/Responder.java @@ -0,0 +1,102 @@ +package com.baeldung.jersey.server; + +import com.baeldung.jersey.server.model.Person; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +@Path("/response") +public class Responder { + + @GET + @Path("/ok") + public Response getOkResponse() { + + String message = "This is a text response"; + + return Response + .status(Response.Status.OK) + .entity(message) + .build(); + } + + @GET + @Path("/not_ok") + public Response getNOkTextResponse() { + + String message = "There was an internal server error"; + + return Response + .status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(message) + .build(); + } + + @GET + @Path("/text_plain") + public Response getTextResponseTypeDefined() { + + String message = "This is a plain text response"; + + return Response + .status(Response.Status.OK) + .entity(message) + .type(MediaType.TEXT_PLAIN) + .build(); + } + + @GET + @Path("/text_plain_annotation") + @Produces({ MediaType.TEXT_PLAIN }) + public Response getTextResponseTypeAnnotated() { + + String message = "This is a plain text response via annotation"; + + return Response + .status(Response.Status.OK) + .entity(message) + .build(); + } + + @GET + @Path("/pojo") + public Response getPojoResponse() { + + Person person = new Person("Abh", "Nepal"); + + return Response + .status(Response.Status.OK) + .entity(person) + .build(); + } + + @GET + @Path("/json") + public Response getJsonResponse() { + + String message = "{\"hello\": \"This is a JSON response\"}"; + + return Response + .status(Response.Status.OK) + .entity(message) + .type(MediaType.APPLICATION_JSON) + .build(); + } + + @GET + @Path("/xml") + @Produces(MediaType.TEXT_XML) + public String sayXMLHello() { + return "" + " This is a xml response "; + } + + @GET + @Path("/html") + @Produces(MediaType.TEXT_HTML) + public String sayHtmlHello() { + return " " + "" + " This is a html title " + "

" + " This is a html response body " + "

" + " "; + } +} \ No newline at end of file diff --git a/jersey/src/main/java/com/baeldung/jersey/server/model/Person.java b/jersey/src/main/java/com/baeldung/jersey/server/model/Person.java new file mode 100644 index 0000000000..98c86bbc3d --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/server/model/Person.java @@ -0,0 +1,32 @@ +package com.baeldung.jersey.server.model; + +public class Person { + String name; + String address; + + public Person(String name, String address) { + this.name = name; + this.address = address; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + @Override + public String toString() { + return "Person [name: " + getName() + " address: " + getAddress() + "]"; + } +} \ No newline at end of file