Partner – Orkes – NPI EA (cat=Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag=Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Partner – LambdaTest – NPI EA (cat=Testing)
announcement - icon

Browser testing is essential if you have a website or web applications that users interact with. Manual testing can be very helpful to an extent, but given the multiple browsers available, not to mention versions and operating system, testing everything manually becomes time-consuming and repetitive.

To help automate this process, Selenium is a popular choice for developers, as an open-source tool with a large and active community. What's more, we can further scale our automation testing by running on theLambdaTest cloud-based testing platform.

Read more through our step-by-step tutorial on how to set up Selenium tests with Java and run them on LambdaTest:

>> Automated Browser Testing With Selenium

Partner – Orkes – NPI EA (cat=Java)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

1. Introduction

Smithy is a way to describe our APIs, along with a set of supporting tooling that enables us to generate API clients and servers from this definition. It allows us to describe our API and then generate client and server code from our definition.

In this tutorial, we’ll take a quick look at the Smithy IDL (Interface Definition Language) and its associated tooling.

2. What Is Smithy?

Smithy is an Interface Definition Language that allows us to describe our APIs in a language and protocol-agnostic format. Amazon originally designed Smithy for describing the AWS APIs, and they’ve since released it publicly so other services can benefit from the same tooling.

We can use the provided tooling to automatically generate server and client SDK code from these API definitions. This then allows us to have the Smithy files serve as the authoritative source of truth for how our API works, ensuring that all client and server code is correct in comparison to this.

Smithy is designed for defining resource-based APIs. The standard use for this is JSON over HTTP, but we can also use other serialisation formats such as XML, and even with non-HTTP transports, such as MQTT.

Smithy can be considered conceptually similar to other standards such as OpenAPI and RAML. However, Smithy takes a much more opinionated approach to how APIs should work – expecting them to be resource-based. At the same time, Smithy doesn’t prescribe how the transport or serialisation should work, allowing more flexibility there.

3. Smithy Files

We define our API by writing .smithy files in the Smithy IDL format. These files define our API in terms of resources, operations that we perform on the resources, and services that represent the entire API boundary.

Our Smithy files start by defining the version of the Smithy IDL format that we’re using and a namespace in which this API will exist:

$version: "2"
// The namespace to use
namespace com.baeldung.smithy.books

We then define our resources, operations, and services within this file, along with other necessary elements, such as the data structures required for operations.

3.1. Resources

The first thing we need to define is our resources. These represent the data we’ll be working with. These are defined using the resource keyword followed by the name of the resource. Within the resource, we define how to identify the resource and outline its associated properties. Later on, we’ll also add the operations that we can perform on the resource.

For example, we can define a resource representing a book:

/// Represents a book in the bookstore
resource Book {
    identifiers: { bookId: BookId }
    properties: {
        title: String
        author: String
        isbn: String
        publishedYear: Integer
    }
}

@pattern("^[a-zA-Z0-9-_]+$")
string BookId

Here, we have a Book resource. This is uniquely identified using the id field, of type BookId – which we’ve separately defined as being a String matching the given format. Our Book additionally has properties for titleauthorisbn, and publishedYear. As such, here’s an example of this resource in the JSON format:

{
    "bookId": "abc123",
    "title": "Head First Java, 3rd Edition: A Brain-Friendly Guide",
    "author": "Kathy Sierra, Bert Bates, Trisha Gee",
    "isbn": "9781491910771",
    "publishedYear": 2022
}

3.2. Services

In addition to resources, our API also requires a service definition. These represent the actual server that’s working with our data. We can have as many of these as we need, each representing a different resource to manage.

We define our service with the service keyword followed by the name of the service. Within this, we then define the version of the service and the resources that it works with:

service BookManagementService {
    version: "1.0"
    resources: [
        Book
    ]
}

At this point, Smithy knows that we have a BookManagementService API that will manage Book resources, but not how to do that.

3.3. Lifecycle Operations

Once we have a resource, we need to be able to perform operations on it. Smithy supports a set of standard lifecycle operations that we can perform:

  • create – Used to create a new instance of the resource, where the service generates the IDs
  • put – Used to create a new instance of the resource, where the client provides the IDs
  • read – Used to retrieve an existing instance of the resource by ID
  • update – Used to update an existing instance of the resource by ID
  • delete – Used to delete an existing instance of the resource by ID
  • list – Used to list instances of the resource

Each operation is defined using the operation keyword and then the name of the operation. Within this, we define the expected input, output and error types.

For example, the operation to get a Book by ID might look like this:

/// Retrieves a specific book by ID
@readonly
operation GetBook {
    input: GetBookInput
    output: GetBookOutput
    errors: [
        BookNotFoundException
    ]
}

/// Input structure for getting a book
structure GetBookInput {
    @required
    bookId: BookId
}

/// Output structure for getting a book
structure GetBookOutput {
    @required
    bookId: BookId

    @required
    title: String

    @required
    author: String

    @required
    isbn: String

    publishedYear: Integer
}

/// Exception thrown when a book is not found
@error("client")
structure BookNotFoundException {
    @required
    message: String
}

The input to this is a single value – the bookId. On success, the output is then the details of our book. Alternatively, we might get an error if the book doesn’t exist.

Notably, although it may seem repetitive, the fields defined in the input and output structures for certain operations must match those in our resource appropriately. However, not all of them need to be included, as we can see from the GetBookInput structure that only has the bookId field.

We then need to indicate that this read operation applies to the Book resource:

resource Book {
    // ...
    read: GetBook
 }

At this point, Smithy now knows that this is an operation we can perform on our resource and how the inputs and outputs should work.

3.4. Non-Lifecycle Operations

Sometimes, we need to have operations that don’t relate to the lifecycle of our resources. For example, we might want an operation to recommend a book to read next.

We define these operations in the same way as lifecycle operations. However, when attaching them to our resource, we need to use the operations keyword instead:

resource Book {
    // ...
    operations: [
        RecommendBook
    ]
}

/// Recommend a book
@readonly
operation RecommendBook {
    input: RecommendBookInput
    output: RecommendBookOutput
}

/// Input structure for recommending a book
structure RecommendBookInput {
    @required
    bookId: BookId
}

/// Output structure for recommending a book
structure RecommendBookOutput {
    @required
    bookId: BookId

    @required
    title: String

    @required
    author: String
}

This then allows us to perform this new operation on our resources.

4. Code Generation

Now that we’ve written our Smithy file to describe our API, we need to be able to build the API itself. Fortunately, Smithy provides tools that automatically generate both client-side SDKs and server-side application services from our Smithy file.

In this article, we’re going to generate Java code. For this, there’s a Gradle plugin that we can use. Unfortunately, there’s no Maven equivalent at the moment, so if we’re going to use Smithy to generate code for our projects, we need to use Gradle as the build tool.

Both client and server code generation use the same Gradle plugin dependencies for smithy-jar and smithy-base, so we first need to ensure we add it to our settings.gradle file:

pluginManagement {
    plugins {
        id 'software.amazon.smithy.gradle.smithy-jar' version "1.3.0"
        id 'software.amazon.smithy.gradle.smithy-base' version "1.3.0"
    }
}

We also need to add the smithy-base plugin to our build.gradle file, as well as ensuring the java-library plugin is present:

plugins {
    id 'java-library'
    id 'software.amazon.smithy.gradle.smithy-base'
}

Next, we need to include the software.amazon.smithy.java.codegen:plugins dependency for the smithyBuild scope:

dependencies {
    smithyBuild "software.amazon.smithy.java.codegen:plugins:0.0.1"
}

Now we ensure that the smithyBuild task runs before the compileJava task:

tasks.named('compileJava') {
    dependsOn 'smithyBuild'
}

Finally, we need to write a smithy-build.json file for the plugin to use. For now, this needs the version of smithy-build – currently “1.0” – and the location of our Smithy files:

{
  "version": "1.0",
  "sources": [
      "./smithy/"
  ]
}

At this point, we’re ready to configure our build for client SDK and/or server code generation.

4.1. Configuring API Protocols

Before we can generate our code, we need to update our Smithy file to indicate the kind of API that we wish to generate. We’ll use the AWS restJson1 protocol.

To do this, we first need to tag our service definition to indicate this is the protocol to use:

@aws.protocols#restJson1
service BookManagementService {
    // ...
}

Next, we tag each operation to specify the HTTP method and URI it uses:

@readonly
@http(method: "GET", uri: "/books/{bookId}")
operation GetBook {
    // ...
}

Here, we’ve indicated that the GetBook operation is exposed using the GET method under the /books/{bookId} URI. The bookId path parameter is taken from the input structure. So, for example, a book with the ID abc123 will be accessed using GET /books/abc123.

4.2. Generating Client SDKs

To generate a client SDK, we need to configure the java-client-codegen plugin in our smithy-build.json file:

{
    ...
    "plugins": {
        "java-client-codegen": {
            "service": "com.baeldung.smithy.books#BookManagementService",
            "namespace": "com.baeldung.smithy.books.client",
            "protocol": "aws.protocols#restJson1"
        },
    }
}

This tells the build to generate code into the com.baeldung.smithy.books.client Java package, and to do so for the BookManagementService within the com.baeldung.smithy.books namespace from our Smithy files.

We also need to add the software.amazon.smithy.java:aws-client-restjson dependency to our build.gradle file to build a client SDK for the AWS restJson1 protocol:

dependencies {
    // ...
    implementation "software.amazon.smithy.java:aws-client-restjson:0.0.1"
}

This then causes the build to generate Java source files in an area under the build directory. To compile these, we then need to tell Gradle about them:

afterEvaluate {
    def clientPath = smithy.getPluginProjectionPath(smithy.sourceProjection.get(), "java-client-codegen")
    sourceSets.main.java.srcDir clientPath
}

Running our build now generates a set of Java classes that we can use to interact with the API. In particular, we’ll obtain both a synchronous and an asynchronous client, as well as DTOs to represent all of our inputs and outputs. These are then immediately ready to use to interact with the API:

BookManagementServiceClient client = BookManagementServiceClient.builder()
  .endpointResolver(EndpointResolver.staticEndpoint("http://localhost:8888"))
  .build();

GetBookOutput output = client.getBook(GetBookInput.builder().bookId("abc123").build());
assertEquals("Head First Java, 3rd Edition: A Brain-Friendly Guide", output.title());

Here we’re using the client to make a call to the service running on http://localhost:8888 and retrieving the title of the book with ID “abc123“.

4.3. Generating Server Stubs

We can generate server stubs in a very similar manner, using the java-server-codegen plugin instead:

{
    ...
    "plugins": {
        "java-server-codegen": {
            "service": "com.baeldung.smithy.books#BookManagementService",
            "namespace": "com.baeldung.smithy.books.server"
        },
    }
}

As before, this generates the code for the BookManagementService within the com.baeldung.smithy.books namespace from our Smithy files into the com.baeldung.smithy.books.server Java package.

We also need to add the software.amazon.smithy.java:aws-server-restjson dependency to our build.gradle file to build server stubs for the AWS restJson1 protocol and the software.amazon.smithy,java:server-netty dependency for the actual HTTP server:

dependencies {
    // ...
    implementation "software.amazon.smithy.java:server-netty:0.0.1"
    implementation "software.amazon.smithy.java:aws-server-restjson:0.0.1"
}

This then causes the build to generate Java source files into an area under the build directory. As before, to compile these, we then need to tell Gradle about them:

afterEvaluate {
    def serverPath = smithy.getPluginProjectionPath(smithy.sourceProjection.get(), "java-server-codegen")
    sourceSets.main.java.srcDir serverPath
}

Running our build now generates a set of Java classes that we can use as stubs for our server. However, this isn’t yet a working server. We also need to set up the server itself and provide implementations of our operations.

Our generated code provides interfaces for each of the operations in our service, as well as classes for the inputs and outputs of these operations:

/**
 * Retrieves a specific book by ID
 */
@SmithyGenerated
@FunctionalInterface
public interface GetBookOperation {
    GetBookOutput getBook(GetBookInput input, RequestContext context);
}

We need to write our own classes that implement each of these interfaces:

class GetBookOperationImpl implements GetBookOperation {
    public GetBookOutput getBook(GetBookInput input, RequestContext context) {
        return GetBookOutput.builder()
          .bookId(input.bookId())
          .title("Head First Java, 3rd Edition: A Brain-Friendly Guide")
          .author("Kathy Sierra, Bert Bates, Trisha Gee")
          .isbn("9781491910771")
          .publishedYear(2022)
          .build();
    }
}

Once we’ve done this, we can then create and start our HTTP server:

Server server = Server.builder()
  .endpoints(URI.create("http://localhost:8888"))
  .addService(
    BookManagementService.builder()
      .addCreateBookOperation(new CreateBookOperationImpl())
      .addGetBookOperation(new GetBookOperationImpl())
      .addListBooksOperation(new ListBooksOperationImpl())
      .addRecommendBookOperation(new RecommendBookOperationImpl())
      .build()
  )
  .build();

server.start();

At this point, we have a fully functional API implementing the specification defined in our Smithy file.

5. Conclusion

In this article, we’ve taken a brief look at Smithy and what we can do with it. We’ve seen how we can use the IDL language to describe our APIs, and then how we can generate both client-side SDKs and server-side stubs from this API definition. We can achieve a lot more using this framework, so the next time you need to create an API, it’s well worth a look.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

Partner – Orkes – NPI EA (cat = Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag = Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – LS – NPI (cat=REST)
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

eBook Jackson – NPI EA – 3 (cat = Jackson)