Getting Started with Solon
Solon is a lightweight Java application framework designed for building web applications, RESTful APIs, and other backend services. It focuses on simplicity, fast startup, low resource usage, and a small programming model.
1. Overview
Modern Java applications often use frameworks to simplify common tasks such as handling HTTP requests, dependency injection, configuration, and application startup. Solon provides these capabilities while keeping the application programming model relatively small and straightforward. Solon can be used to build RESTful APIs, web applications, microservices, and lightweight backend services. It supports familiar Java concepts such as annotations, dependency injection, controllers, and HTTP routing. One of the main ideas behind Solon is to provide the functionality required by an application without forcing developers to introduce a large amount of framework-specific complexity. In this article, we will create a simple RESTful API using Solon. The example will expose an endpoint that returns information about a product.
2. What Is Solon
Solon is a Java framework for developing lightweight applications and services. It provides an application runtime along with features such as HTTP routing, dependency injection, configuration, and web application development. A Solon application can be started with a small Java class. HTTP endpoints can then be defined using annotations such as @Controller and @Mapping. The basic flow of a Solon web application is:
- The Solon application starts.
- The framework discovers application components and controllers.
- HTTP routes are registered.
- A client sends an HTTP request.
- Solon invokes the matching controller method.
- The method result is returned as the HTTP response.
For example, a controller can define a GET endpoint using:
@Mapping("/products")
public class ProductController {
@Mapping("/1")
public Product getProduct() {
return new Product(1, "Laptop", 75000);
}
}
When a client sends a request to /products/1, Solon invokes the corresponding method and converts the returned Java object into the HTTP response.
2.1 Why Use Solon?
Solon can be useful when an application needs a lightweight Java web framework. Some of its important characteristics are:
- Simple programming model
- Lightweight runtime
- Annotation-based routing
- Dependency injection support
- RESTful API development
- Suitable for microservices and backend applications
2.2 Solon vs Spring Boot
Solon and Spring Boot can both be used to build Java web applications and RESTful APIs, but they take different approaches. Spring Boot provides a large ecosystem and extensive production features, while Solon focuses more on a lightweight programming model and low resource usage.
| Aspect | Solon | Spring Boot |
|---|---|---|
| Programming model | Lightweight and annotation-based | Annotation-based with a broader application ecosystem |
| Footprint | Designed for lightweight applications and services | Generally larger due to its extensive ecosystem and auto-configuration |
| Startup | Designed for fast startup and low resource usage | Startup time depends on the application and number of dependencies |
| Use cases | REST APIs, microservices, lightweight backend services, and web applications | REST APIs, microservices, enterprise applications, and large backend systems |
| Ecosystem | Smaller and focused | Large ecosystem with extensive libraries and integrations |
The choice depends on the requirements of the application. Spring Boot can be a strong choice when an application needs the extensive Spring ecosystem, while Solon can be considered when a lightweight framework and simple application model are important.
2.3 When to Use Solon
Solon can be considered when you want to build a Java application without introducing unnecessary framework complexity. Some common scenarios include:
- RESTful APIs: Solon provides a simple way to define HTTP endpoints and return Java objects as API responses.
- Microservices: Its lightweight approach can be useful for small independent services where startup time and resource usage matter.
- Lightweight backend services: Solon is suitable for applications that need HTTP handling, dependency injection, and configuration without a large application stack.
- Small to medium-sized applications: The annotation-based programming model keeps basic web applications relatively simple.
- Cloud and container-based applications: Applications that are frequently started, stopped, or scaled can benefit from a lightweight runtime.
Solon is not necessarily a replacement for every Java application framework. For applications that depend heavily on Spring-specific libraries, integrations, or enterprise features, Spring Boot may be the more practical choice. For simpler services where a smaller programming model is preferred, Solon is worth considering. The framework also supports different application styles, allowing developers to choose the features required by their application instead of introducing unnecessary complexity.
3. RESTful API Example
Let’s create a simple RESTful API using Solon. The application will expose a GET endpoint: GET /products/1. The endpoint will return the details of a product as JSON.
3.1 Maven Dependency
A Solon application can be created using Maven. The Solon web dependency provides the required functionality for building the HTTP application.
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-web</artifactId>
<version>stable__jar__version</version>
</dependency>
The exact Solon version should be aligned with the version currently used by your project. The example below focuses on the application programming model rather than Maven configuration.
3.2 Product Class
First, create a simple Java class representing a product.
public class Product {
private int id;
private String name;
private double price;
public Product(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
The Product class contains three properties: id, name, and price. These values will be converted into JSON when the object is returned from the REST endpoint.
3.3 REST Controller
Next, create a controller that exposes the REST endpoint.
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Mapping;
@Controller@Mapping("/products")
public class ProductController {
@Mapping("/1")
public Product getProduct() {
return new Product(1, "Laptop", 75000);
}
}
The @Controller annotation tells Solon that the class is a web controller. The class-level @Mapping("/products") defines the base URL for the controller. The method-level @Mapping("/1") adds the remaining part of the URL. Therefore, the complete endpoint becomes: GET /products/1. The method returns a Product object. Solon handles the HTTP response and serialization of the returned object.
3.4 Application Class
The application needs a main class to start the Solon runtime.
import org.noear.solon.Solon;
public class Application {
public static void main(String[] args) {
Solon.start(Application.class, args);
}
}
The Solon.start() method starts the application. During startup, Solon discovers the application components and registers the controller and its HTTP mappings.
3.5 Code Run and Output
After creating the classes and adding the Maven dependency, run the Application class. Solon starts the application and exposes the REST endpoint on the default HTTP port. Once the application is running, call the endpoint using a browser, curl, or any REST client:
curl http://localhost:8080/products/1
The request is mapped to the getProduct() method in ProductController. The method returns a Product object, which Solon serializes into a JSON response. The output is:
{
"id": 1,
"name": "Laptop",
"price": 75000.0
}
The id, name, and price values in the response come from the Product object created by the controller. This demonstrates how Solon maps an HTTP request to a Java method and returns the method result as JSON.
4. Conclusion
Solon is a lightweight Java framework that can be used to build RESTful APIs, web applications, and backend services. It provides features such as HTTP routing, dependency injection, and application configuration while keeping the programming model simple. In this example, @Controller was used to define a REST controller and @Mapping was used to map an HTTP request to a Java method. The returned Product object was then converted into a JSON response by Solon. The example also shows that a basic REST API can be created with a small amount of Java code, making Solon a useful option for lightweight services and microservices where simplicity and low resource usage are important.




