Skip to content

[Code Update - Chapter 11] Code update for MP Rest Client 4.0#73

Open
ttelang wants to merge 7 commits into
microprofile:mainfrom
ttelang:code-update-mp-rest-client-4.0
Open

[Code Update - Chapter 11] Code update for MP Rest Client 4.0#73
ttelang wants to merge 7 commits into
microprofile:mainfrom
ttelang:code-update-mp-rest-client-4.0

Conversation

@ttelang
Copy link
Copy Markdown
Contributor

@ttelang ttelang commented Apr 24, 2026

Description: Code Update for MicroProfile Rest Client 4.0

Overview

This PR updates Chapter 11 of the MicroProfile tutorial to demonstrate the latest features of MicroProfile Rest Client 4.0 alongside Jakarta EE 10 and MicroProfile 7.1. The changes include comprehensive code examples, extensive documentation, and new service implementations showcasing best practices for building type-safe REST clients in microservices architectures.

Key Updates

MicroProfile Rest Client 4.0 Features Demonstrated

1. New baseUri(String) Convenience Method

  • Eliminates the need for URI.create() wrapper
  • Direct string parameter support for simpler API usage
  • Demonstrated in InventoryService for programmatic client creation

2. CDI Integration with @RestClient

  • Mandatory @RestClient qualifier for CDI injection
  • Automatic implementation generation by the runtime
  • Dependency injection of REST clients in services

3. AutoCloseable Support

  • REST client interfaces extend AutoCloseable
  • Enables try-with-resources pattern for automatic resource cleanup
  • Essential for programmatic client creation with RestClientBuilder

📝 Code Examples

REST Client Interface (ProductClient.java)

  • Demonstrates @RegisterRestClient annotation
  • Type-safe method definitions with Jakarta REST annotations
  • @RegisterProvider for custom exception mapping
  • Comprehensive JavaDoc with usage examples

Exception Handling (New Files)

  • ProductNotFoundException.java - Checked exception (404 responses)
  • ServiceUnavailableException.java - Unchecked exception (503 responses)
  • ProductServiceResponseExceptionMapper.java - Maps HTTP errors to custom exceptions

Custom Filters (New Filter Classes)

  • BearerTokenFilter.java - Authentication via Bearer tokens
  • CorrelationIdFilter.java - Distributed tracing with correlation IDs
  • RequestLoggingFilter.java - Comprehensive request logging
  • ResponseLoggingFilter.java - Response logging with status tracking
  • ProductClientWithFilters.java - Client with all filters registered

Service Classes

  • ProductCatalogService.java - CDI-injected REST client usage
  • ProductClientBuilderService.java - Programmatic client creation examples
  • FilteredProductCatalogService.java - Service using filtered clients

REST Resource (ProductCatalogResource.java)

  • Comprehensive REST endpoints (853 lines) demonstrating:
    • Basic REST client usage
    • Exception mapping
    • RestClientBuilder for programmatic client creation
    • Custom filters and interceptors
    • OpenAPI documentation via annotations

Documentation Enhancements

The README.adoc now includes extensive sections:

  • Learning Objectives summarizing what developers will understand
  • curl commands for testing
  • step-by-step verification Instructions
  • Filter Chain Demonstrations with expected log output
  • Troubleshooting Guide for common issues

Use Cases Covered

  1. Basic REST client registration and CDI injection
  2. Configuration via MicroProfile Config properties
  3. Parameter annotations (@PathParam, @QueryParam)
  4. Custom exception mapping with ResponseExceptionMapper
  5. Checked vs unchecked exception handling
  • Comprehensive error handling strategies
  1. Custom request/response filters
  2. Filter priority and execution order
  3. Programmatic client creation with RestClientBuilder
  4. Dynamic configuration at runtime
  5. Environment-specific client configuration

Technology Stack Upgrades

  • Java: 17 → 21 (across all services)
  • MicroProfile: 6.1 → 7.1 (including MP Rest Client 4.0)
  • Open Liberty: 23.0.0.3 → 25.0.0.1
  • Maven: 3.6+ → 3.9+
  • Lombok: 1.18.26 → 1.18.36

Testing Guidance

Quick Start:

# Terminal 1 - Catalog Service
cd code/chapter11/catalog
mvn liberty:run

# Terminal 2 - Payment Service
cd code/chapter11/payment
mvn liberty:run

Testing Endpoints:

  • CDI-injected: GET /catalog/products, GET /catalog/products/{id}
  • Programmatic: GET /catalog/builder/products, GET /catalog/builder/products/{id}/dynamic
  • With Filters: GET /catalog/filtered/products, GET /catalog/compare/{id}

@ttelang ttelang changed the title Code update mp rest client 4.0 [Code Update - Chapter 11] Code update for MP Rest Client 4.0 Apr 24, 2026
@ttelang ttelang marked this pull request as ready for review April 24, 2026 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant