Lecture 10 - Implementing REST Flashcards
Is Jakarta RESTful Web Services (JAX-RS) part of the Jakarta EE spec?
Yes
What JAX-RS annotation defines URI mappings and templates?
@Path
What 2 JAX-RS annotations define the resource to produce and consume?
@Produces
@Consumes
What 5 JAX-RS annotations identifies which HTTP method a Java method is interested in?
@GET
@POST
@DELETE
@PUT
@HEADER
What is a JAX-RS parameter annotation?
In JAX-RS (Java API for RESTful Web Services), parameter annotations are used to extract data from an HTTP request and pass it to resource methods.
@PathParam
@QueryParam
@HeaderParam
@CookieParam
@MatrixParam
@Context (deprecated)
Example of a form that uses JAX-RS
@Path(“/categories”)
public interface CategoryService {
@GET
@Path(“{id}”)
@Produces(MediaType.APPLICATION_JSON)
Category find(@PathParam(“id”) int id);
@POST
@Consumes(MediaType.APPLICATION_JSON)
void persist(Category category);
@PUT
@Consumes(MediaType.APPLICATION_JSON)
void merge(Category category);
@DELETE
@Path(“{id}”)
void remove(@PathParam(“id”) int id);
@GET
@Produces(MediaType.APPLICATION_JSON)
Category[] getAll();
}
How to GET and POST work?
GET Requests: Data is passed through the URI as path parameters or query parameters.
Path parameters are embedded directly within the URL path (e.g., /users/{id})
POST Requests: Data is usually not passed in the URI but instead in the request body.
This is often the case when submitting forms with more complex data, such as JSON objects.
The URI is still used to specify the endpoint (e.g., /users), but the actual data is placed in the request body rather than in the URL.
What is a JAX-RS Response and ResponseBuilder class used for?
Customizes HTTP response messages that go back to client (ex: 404 not found)
How do we set the context root of our RESTful service (2 ways)?
Extend jakarta.ws.rs.core.Application
Annotate class with @ApplicationPath(“/api”)
OR
Use servlet-mapping tag in web.xml
<servlet-mapping>
<servlet-name>
jakarta.ws.rs.core.Application
...
</servlet-name></servlet-mapping>
What beans can JAX-RS annotations be used on?
Stateless session
Singleton session
NOT stateful session beans
What are MessageBodyReader/Writers?
In JAX-RS (Java API for RESTful Web Services), MessageBodyReader and MessageBodyWriter are interfaces that handle the serialization and deserialization of HTTP request and response bodies. These are essential for converting data between Java objects and HTTP message bodies (e.g., JSON, XML, or text), enabling easy data exchange between the client and server.
How do we set up a client object for use in our back end?
Client client = ClientBuilder.newClient();
Response res = client.target(“http://example.org/hello”)
.request(“text/plain”).get();
What 3 security services are provided with RESTful services?
Authentication, authorization, encryption
How do we enforce security in JAX-RS?
<security-constraint> element in web.xml
</security-constraint>