Skip to content

Commit b9fa7e1

Browse files
committed
more content
1 parent 48c54e2 commit b9fa7e1

File tree

1 file changed

+117
-11
lines changed

1 file changed

+117
-11
lines changed

README.asciidoc

+117-11
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,42 @@ Persistence
2222
-----------
2323

2424
* Right-click on project, select ``Properties'', search for ``facet'', enable ``JPA'', click on ``Apply'', talk about ``Further configuration available'' and default data source
25-
* Create a new entity
26-
* Add a primary key in the wizard. Use `long` datatype and name as `id`.
25+
* Create a new entity `Student`
26+
* Add a primary key in the wizard. Use `long` datatype and `id` name.
2727
* Generate Getter/Setter by clicking ``Source'', ``Getters and Setters''.
28+
* Add `toString` implementation
29+
* The updated class should look like:
30+
+
31+
[source, java]
32+
----
33+
@Entity
34+
public class Student implements Serializable {
35+
36+
@Id
37+
private long id;
38+
private static final long serialVersionUID = 1L;
39+
40+
public Student() {
41+
super();
42+
}
43+
44+
public Student(long id) {
45+
this.id = id;
46+
}
47+
public long getId() {
48+
return this.id;
49+
}
50+
51+
public void setId(long id) {
52+
this.id = id;
53+
}
54+
55+
public String toString() {
56+
return "Student[" + id + "]";
57+
}
58+
}
59+
----
60+
+
2861
* Add the following properties to `persistence.xml`:
2962
+
3063
[source.xml]
@@ -55,21 +88,94 @@ curl -L https://github.com/jboss-developer/jboss-eap-quickstarts/blob/6.3.0.GA/h
5588
+
5689
* Start the server and access http://localhost:8080/h2console
5790
* Enter the JDBC URL and credentials. To access the "test" database your application uses, enter these details (everything else is default):
58-
** JDBC URL: jdbc:h2:mem:test;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
59-
** User Name: sa
60-
** Password: sa
91+
** JDBC URL: `jdbc:h2:mem:test;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1`
92+
** User Name: `sa`
93+
** Password: `sa`
94+
95+
RESTful Web Services
96+
--------------------
97+
98+
* Create a JAX-RS resource `StudentEndpoint`
99+
** Use `Student` entity as the basis, select `create`, `findById`, `listAll` methods to be generated.
100+
* Add `@XmlRootElement` in `Student` entity to enable XML <-> Java conversion.
101+
* Add `@NamedQuery(name="findAllStudents", query="select s from Student s")` to `Student` to enable querying all students.
102+
* Update the class so that it looks like as shown:
103+
+
104+
[source,java]
105+
----
106+
@RequestScoped
107+
@Path("/students")
108+
public class StudentEndpoint {
109+
110+
@PersistenceContext EntityManager em;
61111
62-
**
112+
@Transactional
113+
@POST
114+
@Consumes({ "application/xml", "application/json", "text/plain" })
115+
public void create(final long id) {
116+
Student student = new Student(id);
117+
em.persist(student);
118+
}
63119
120+
@GET
121+
@Path("/{id:[0-9][0-9]*}")
122+
@Produces({ "application/xml", "application/json" })
123+
public Response findById(@PathParam("id") final Long id) {
124+
Student student = em.find(Student.class, id);
125+
if (student == null) {
126+
return Response.status(Status.NOT_FOUND).build();
127+
}
128+
return Response.ok(student).build();
129+
}
130+
131+
@GET
132+
@Produces("application/xml")
133+
public Student[] listAll(
134+
@QueryParam("start") final Integer startPosition,
135+
@QueryParam("max") final Integer maxResult) {
136+
TypedQuery<Student> query = em.createNamedQuery("findAllStudents", Student.class);
137+
final List<Student> students = query.getResultList();
138+
return students.toArray(new Student[0]);
139+
}
140+
}
141+
----
142+
+
143+
* Use ``Advanced REST Client'' in Chrome
144+
** Make a GET request
145+
*** Add Accept: application/xml
146+
*** Show empty response
147+
** Make a POST request
148+
*** Payload as `1`
149+
*** ``Content-Type'' header to `text/plain`
150+
** Make a GET request to validate the data is posted
151+
** Change `create` method to add Bean Validation constraint
152+
+
153+
[source,java]
154+
----
155+
public void create(@Min(10) final long id) {
156+
----
157+
+
158+
** Make a POST request with payload as `1` and show an error is being received
159+
* Add a new Servlet and the following code in `doGet`
160+
+
161+
[source,java]
162+
----
163+
ServletOutputStream out = response.getOutputStream();
164+
out.print("Retrieving results ...");
165+
Client client = ClientBuilder.newClient();
166+
Student[] result = client
167+
.target("http://localhost:8080/helloworld/rest/students")
168+
.request()
169+
.get(Student[].class);
170+
for (Student s : result) {
171+
out.print(s.toString());
172+
}
173+
----
174+
* Access the Servlet in the browser to show the results
64175
65176
66177
* Basic Maven project creation
67178
** Search for `wildfly-javaee7` archetype - there are 4, which one to choose and when ?
68-
* JAX-RS
69-
** Wizards
70-
** Content assist
71-
** Validation
72-
** Navigation
73179
* CDI
74180
** Wizards:
75181
http://docs.jboss.org/tools/4.1.x.Final/en/cdi_tools_reference_guide/html/chap-CDI_Tools_Reference_Guide-Creating_a_CDI_Web_Project.html[New CDI Web Project Wizard],

0 commit comments

Comments
 (0)