UNIT - 3
Web Sites for Non-Browsers: JAX-RS
Building RESTful Services for Programmatic Clients
https://ravulakartheek.blogspot.com/
https://ravulakartheek.blogspot.com/
Introduction
https://ravulakartheek.blogspot.com/
https://ravulakartheek.blogspot.com/
What Is JAX-RS?
https://ravulakartheek.blogspot.com/
https://ravulakartheek.blogspot.com/
Why Non-Browser Clients?
https://ravulakartheek.blogspot.com/
https://ravulakartheek.blogspot.com/
REST vs. Traditional Web Pages
Traditional Web | RESTful Service |
HTML pages | JSON/XML data |
Designed for browsers | Designed for apps |
UI-driven | API-driven |
Sessions | Stateless |
https://ravulakartheek.blogspot.com/
https://ravulakartheek.blogspot.com/
Core JAX-RS Annotations
https://ravulakartheek.blogspot.com/
https://ravulakartheek.blogspot.com/
Example REST Resource
@Path("/hello")
public class HelloResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Map<String, String> hello() {
return Map.of("message", "Hello, Non-Browser Client!");
}
}
https://ravulakartheek.blogspot.com/
https://ravulakartheek.blogspot.com/
Content Negotiation
Server decides the best match based on:
https://ravulakartheek.blogspot.com/
https://ravulakartheek.blogspot.com/
Returning JSON
@GET
@Produces(MediaType.APPLICATION_JSON)
public User getUser() {
return new User("Alice", 25);
}
https://ravulakartheek.blogspot.com/
https://ravulakartheek.blogspot.com/
Handling Parameters
Path parameters:
@GET
@Path("/{id}")
public User getUser(@PathParam("id") int id) { ... }
Query parameters:
@GET
public List<User> search(@QueryParam("name") String name) { ... }
https://ravulakartheek.blogspot.com/
https://ravulakartheek.blogspot.com/
POST Example (Create Resource)
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createUser(User user) {
service.save(user);
return Response.status(201).build();
}
https://ravulakartheek.blogspot.com/
https://ravulakartheek.blogspot.com/
Tools for Testing Non-Browser APIs
https://ravulakartheek.blogspot.com/
https://ravulakartheek.blogspot.com/
Best Practices
https://ravulakartheek.blogspot.com/
https://ravulakartheek.blogspot.com/
Summary
https://ravulakartheek.blogspot.com/
https://ravulakartheek.blogspot.com/