1 of 14

UNIT - 3

Web Sites for Non-Browsers: JAX-RS

Building RESTful Services for Programmatic Clients

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

2 of 14

Introduction

  • Not all clients are web browsers
  • Applications, scripts, IoT devices, and mobile apps often need machine-readable data
  • JAX-RS provides a standard for building RESTful APIs in Java
  • Purpose: Show how to design “web sites for non-browsers” using JAX-RS

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

3 of 14

What Is JAX-RS?

  • Java API for RESTful Web Services
  • Part of the Jakarta EE ecosystem
  • Uses annotations to simplify development
  • Provides automatic content negotiation (JSON, XML, etc.)

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

4 of 14

Why Non-Browser Clients?

  • Integration with external systems
  • Mobile and desktop apps
  • Microservices communication
  • Automated processes (cron jobs, data pipelines)
  • IoT devices consuming REST endpoints

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

5 of 14

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/

6 of 14

Core JAX-RS Annotations

  • @Path – define resource routes
  • @GET, @POST, @PUT, @DELETE – HTTP method handlers
  • @Produces – define output format
  • @Consumes – define expected input format
  • @PathParam, @QueryParam, @HeaderParam – parameter injection

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

7 of 14

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!");

}

}

  • Clients receive JSON data
  • Ideal for apps, scripts, and devices

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

8 of 14

Content Negotiation

  • JAX-RS supports automatic selection via HTTP headers:
  • Client example:
  • Accept: application/json
  • Accept: application/xml

Server decides the best match based on:

    • @Produces
    • Client header preferences

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

9 of 14

Returning JSON

  • Most common format for non-browser apps.

@GET

@Produces(MediaType.APPLICATION_JSON)

public User getUser() {

return new User("Alice", 25);

}

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

10 of 14

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/

11 of 14

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/

12 of 14

Tools for Testing Non-Browser APIs

  • curl / httpie
  • Postman
  • Insomnia
  • Mobile apps
  • Shell scripts
  • Python scripts
  • IoT devices (ESP32, Raspberry Pi, etc.)

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

13 of 14

Best Practices

  • Use proper HTTP status codes

  • Validate input

  • Provide consistent JSON structures

  • Implement error handling

  • Document with OpenAPI/Swagger

  • Secure endpoints (JWT, OAuth2, API keys)

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

14 of 14

Summary

  • Non-browser clients rely on machine-readable APIs

  • JAX-RS provides a clean, annotation-based approach to REST

  • Supports JSON, XML, and content negotiation

  • Ideal for mobile, IoT, and server integrations

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/