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

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.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

1. Overview

In this article, we’ll have a look at VRaptor, a simple and straightforward Java MVC web framework which utilizes Java Contexts and Dependency Injection technology and is easy to grasp.

Just like Spring – it relies heavily on annotations and works great with Hibernate.

It also comes with some useful plugins – such as for internalization and unit testing.

So, let’s explore the different components of VRaptor and create a sample project.

2. Maven Dependencies and Setup

One quick way to get up and running is to download the vraptor-blank-project-distribution from the official repository.

The blank project is just a skeleton that can be fleshed out to become a full-fledged web application of choice.

After downloading and unzipping the project, let’s rename the directory to vraptor (or any other name).

The directory should contain:

  • src/
  • pom.xml
  • and README.md

The project is Maven-based and comes with tomcat7 Maven plugin which provides the servlet container for running the application.

It also comes with a default IndexController that has just one method – index().

By default, the view to be rendered by this method is located in webapp/WEB-INF/jsp/index/index.jsp – this follows the convention WEB-INF/jsp/controller_name/method_name.

To start the server, we’ll execute the command mvn tomcat7:run from the root of the project.

If successful, if we visit http://localhost:8080, a browser will display “It works!! VRaptor!“.

If we face the “java.lang.LinkageError: loader constraint violation”, then, we have to modify the following dependencies in pom.xml:

<dependency>
    <groupId>org.jboss.weld.servlet</groupId>
    <artifactId>weld-servlet-core</artifactId>
    <version>2.1.2.Final</version>
    <exclusions>
        <exclusion>
	    <groupId>org.jboss.spec.javax.el</groupId>
	    <artifactId>jboss-el-api_3.0_spec</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.jboss.weld</groupId>
    <artifactId>weld-core-impl</artifactId>
    <version>2.1.2.Final</version>
    <exclusions>
       <exclusion>
          <groupId>org.jboss.spec.javax.el</groupId>
  	  <artifactId>jboss-el-api_3.0_spec</artifactId>
       </exclusion>
    </exclusions>
</dependency>

The culprit is the el-api that is included in weld-servlet-core and weld-core-impl with compile scope; this leads to a dependency clash.

The following dependencies will be needed along the line, so let’s include them in pom.xml:

<dependency>
    <groupId>br.com.caelum.vraptor</groupId>
    <artifactId>vraptor-freemarker</artifactId>
    <version>4.1.0-RC3</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.8-dmr</version>
</dependency>

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.27-incubating</version>
</dependency>

The latest version of vraptor-freemarker, mysql-connector-java, and freemarker artifacts can be found in Maven Central.

Now that we’re good to go, let’s build a simple blog site.

3. Hibernate Support

VRaptor provides various plugins for interacting with databases, one of them is vraptor-hibernate which works with Hibernate 4.

The plugin makes the Hibernate’s SessionFactory bean available at runtime via CDI.

With the plugin in place, we need a standard Hibernate configuration file – an example can be found in the repository.

VRaptor uses a technique called Producers to make objects available for DI management. More details about this here.

4. Defining Web Routes in VRaptor

In VRaptor, route definitions reside in controllers which are simply @Controller-annotated Java objects – just like in Spring.

@Path annotation is used for mapping a request path to a particular controller and @Get, @Post, @Put, @Delete and @Patch annotations are used for specifying HTTP request types.

Route mapping configuration looks similar to JAX-RS’ way but doesn’t implement the standard officially.

Additionally, when defining a path, it’s possible to specify a path variable in curly braces:

@Get("/posts/{id}")

The value of id can then be accessed inside a controller method:

@Get("/posts/{id}")
public void view(int id) {
    // ...
}

When a form is submitted to a particular route, VRaptor can automatically populate an object with the form-data submitted.

Let’s see this in action in the next section of the article.

5. Views and Template Engine

By default, views can be implemented using JSP. However, other template engines can be used as well – in this article, we’ll work with Freemarker.

Let’s start by creating index.ftl and saving it in the default view directory (src/main/resources/templates):

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>VRaptor Blank Project</title>
</head>
<body>
It works!! ${variable}
</body>
</html>

Now, we can use the defined view with a FreemarkerView class for view rendering:

@Path("/")
public void index() {
    result.include("variable", "VRaptor!");
    result.use(FreemarkerView.class).withTemplate("index");
}

The Result object holds model state – it has methods for redirecting to another page, URL, or a controller method; it can be injected into the controller using CDI.

In our example, the variable gets resolved by Freemarker. Thus ${variable} placeholder in index.ftl gets replaced with the “VRaptor!”.

More advanced usages are documented here.

6. Form Submission Handling Example

Let’s see how can we handle form submissions with validation:

@Post("/post/add")
public void add(Post post) {
    post.setAuthor(userInfo.getUser());
    validator.validate(post);
    if(validator.hasErrors()) {
        result.include("errors", validator.getErrors());
    }
    validator.onErrorRedirectTo(this).addForm();
  
    Object id = postDao.add(post);
  
    if(Objects.nonNull(id)) {
       result.include("status", "Post Added Successfully");
         result.redirectTo(IndexController.class).index();
    } else {
        result.include(
          "error", "There was an error creating the post. Try Again");
        result.redirectTo(this).addForm();
    }
}

The Post object gets validated first using Java bean validation before being persisted into the database using postDao.add().

The fields of the Post object are populated automatically from values of the submitted form-data – which correspond to the form’s input fields in the view file.

Note that the name of the input field has to be prefixed with the name of the object in lowercase.

For example, the view that’s responsible for adding a new post has input fields: post.title and post.post which correspond to fields title and post in Post.java respectively:

<input type="text" class="form-control" placeholder="Title" 
  id="title" name="post.title" required />

<textarea rows="10" class="form-control" placeholder="Post" 
  id="post" name="post.post" required></textarea>

The complete add.ftl file can be found in the source code.

If there is an error in the form submission, the error message gets included and user redirected to the same add() method:

if(validator.hasErrors()) {
    result.include("errors", validator.getErrors());
}
validator.onErrorRedirectTo(this).addForm();

7. Conclusion

In conclusion, we’ve looked at VRaptor at a glance and saw how basic MVC functionality can be achieved.

The documentation contains more detail about the framework as well as available plugins.

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.

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)