Skip to content

Commit 8cbca2f

Browse files
abh-swarpivovarit
authored andcommitted
Code check-in for http://jira.baeldung.com/browse/BAEL-2012 (Set a Response body in JAX-RS) (eugenp#5009)
* abh.swar@gmail.com: Code check-in for article on Spring WebFlux 1. EmailWebClient is the client that subscribes to the data from WebFlux server 2. EmailGenerator generates one email per second randomly 3. EmailHandler and EmailRouter deal with handling of request of the subscriber 4. Email is the POJO for data transmitted by the server * Code check-on for (http://jira.baeldung.com/browse/BAEL-1897) How to increment a Date by one day - Added code and test to increment date by one day using java 8 and joda-time - Added joda-time 2.10 dependency in pom.xml * Revert "abh.swar@gmail.com: Code check-in for article on Spring WebFlux 1. EmailWebClient is the client that subscribes to the data from WebFlux server 2. EmailGenerator generates one email per second randomly 3. EmailHandler and EmailRouter deal with handling of request of the subscriber 4. Email is the POJO for data transmitted by the server" This reverts commit 6254ad9 * Code check-on for (http://jira.baeldung.com/browse/BAEL-1897) How to increment a Date by one day - Added code and test to increment date by one day using java.util.Calendar and ApacheCommons and unit tests for it - Used properties for ${joda.version} - Formatted the code using IntelliJ formatter - Renamed DateIncrementerTest to DateIncrementerUnitTest - Changed test method names to follow _given_when_then convention * Code check-on for (http://jira.baeldung.com/browse/BAEL-1897) How to increment a Date by one day - Removed unnecessary comment * Code check-on for (http://jira.baeldung.com/browse/BAEL-1897) How to increment a Date by one day - Corrected the order of parameters of assertEquals() method * Code check-in for http://jira.baeldung.com/browse/BAEL-2012 (Set a Response body in JAX-RS) - Added a Responder resource that has all the REST endpoints - Added a POJO Person which will be used for JSON serialization * Create test * Delete test
1 parent 5e3a0c7 commit 8cbca2f

2 files changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.baeldung.jersey.server;
2+
3+
import com.baeldung.jersey.server.model.Person;
4+
5+
import javax.ws.rs.GET;
6+
import javax.ws.rs.Path;
7+
import javax.ws.rs.Produces;
8+
import javax.ws.rs.core.MediaType;
9+
import javax.ws.rs.core.Response;
10+
11+
@Path("/response")
12+
public class Responder {
13+
14+
@GET
15+
@Path("/ok")
16+
public Response getOkResponse() {
17+
18+
String message = "This is a text response";
19+
20+
return Response
21+
.status(Response.Status.OK)
22+
.entity(message)
23+
.build();
24+
}
25+
26+
@GET
27+
@Path("/not_ok")
28+
public Response getNOkTextResponse() {
29+
30+
String message = "There was an internal server error";
31+
32+
return Response
33+
.status(Response.Status.INTERNAL_SERVER_ERROR)
34+
.entity(message)
35+
.build();
36+
}
37+
38+
@GET
39+
@Path("/text_plain")
40+
public Response getTextResponseTypeDefined() {
41+
42+
String message = "This is a plain text response";
43+
44+
return Response
45+
.status(Response.Status.OK)
46+
.entity(message)
47+
.type(MediaType.TEXT_PLAIN)
48+
.build();
49+
}
50+
51+
@GET
52+
@Path("/text_plain_annotation")
53+
@Produces({ MediaType.TEXT_PLAIN })
54+
public Response getTextResponseTypeAnnotated() {
55+
56+
String message = "This is a plain text response via annotation";
57+
58+
return Response
59+
.status(Response.Status.OK)
60+
.entity(message)
61+
.build();
62+
}
63+
64+
@GET
65+
@Path("/pojo")
66+
public Response getPojoResponse() {
67+
68+
Person person = new Person("Abh", "Nepal");
69+
70+
return Response
71+
.status(Response.Status.OK)
72+
.entity(person)
73+
.build();
74+
}
75+
76+
@GET
77+
@Path("/json")
78+
public Response getJsonResponse() {
79+
80+
String message = "{\"hello\": \"This is a JSON response\"}";
81+
82+
return Response
83+
.status(Response.Status.OK)
84+
.entity(message)
85+
.type(MediaType.APPLICATION_JSON)
86+
.build();
87+
}
88+
89+
@GET
90+
@Path("/xml")
91+
@Produces(MediaType.TEXT_XML)
92+
public String sayXMLHello() {
93+
return "<?xml version=\"1.0\"?>" + "<hello> This is a xml response </hello>";
94+
}
95+
96+
@GET
97+
@Path("/html")
98+
@Produces(MediaType.TEXT_HTML)
99+
public String sayHtmlHello() {
100+
return "<html> " + "<title>" + " This is a html title </title>" + "<body><h1>" + " This is a html response body " + "</body></h1>" + "</html> ";
101+
}
102+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.jersey.server.model;
2+
3+
public class Person {
4+
String name;
5+
String address;
6+
7+
public Person(String name, String address) {
8+
this.name = name;
9+
this.address = address;
10+
}
11+
12+
public String getName() {
13+
return name;
14+
}
15+
16+
public void setName(String name) {
17+
this.name = name;
18+
}
19+
20+
public String getAddress() {
21+
return address;
22+
}
23+
24+
public void setAddress(String address) {
25+
this.address = address;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "Person [name: " + getName() + " address: " + getAddress() + "]";
31+
}
32+
}

0 commit comments

Comments
 (0)