Using Webjars in Spring Boot application

Posted on December 9, 2017

Webjars are jar (Java Archive) files, which contain client side libraries such as JQuery, jQuery UI, Bootstrap, CoffeeScript etc. You can use the build tool (Eg. Maven, Gradle etc) to add the Webjars dependencies in your Spring Boot application. It helps to avoid the traditional way of using the static libraries for client side development.

This post shows you how to use the Webjars in Spring Boot application for client side development.

Tools and technologies used for this application are -

  • Spring Boot 1.5.9.RELEASE
  • Java SE 1.8
  • Maven 3.5.2
  • Eclipse Neon.3 (4.6.3)

Project structure

Final project structure of our application will look like as follows.


Jar dependencies

In this example, we will use the Bootstrap theme, component etc. in our views (i.e. JSP) from Webjars.

To use Webjars in your application, add the Webjars dependencies in your pom.xml file as follows.


<project xmlns="" xmlns:xsi=""
  <name>Spring Boot - WebJars Example</name>
    <!-- Spring Starter for Web application -->

    <!-- Jasper for rendering jsp in Embedded Tomcat -->

    <!-- JSTL tag library -->
    <!-- Webjars for Bootstrap -->


Using Webjars in Views

You can use the /webjars/** path for static contents, served from Webjars. For example, use the following paths for bootstrap theme and JavaScript’s.

<!--Bootstrap CSS-->
<link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css"> 
<!--Bootstrap JS-->
<script src="/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js"></script>

You can explore the same in Webjars jar as follows.



Here is a complete example of using Webjars for bootstrap in JSP view.

Create an index.jsp file under src/main/webapp/WEB-INF/views folder as shown in the project structure.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<!--  Static resources form Webjars -->
<link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css">
<script src="/webjars/jquery/1.11.1/jquery.min.js"></script>
<script src="/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js"></script>

<title>Spring Boot</title>
   <div class="container">
      <div class="header clearfix">
            <ul class="nav nav-pills pull-right">
               <li role="presentation" class="active"><a href="#">Home</a></li>
               <li role="presentation"><a href="#">About</a></li>
               <li role="presentation"><a href="#">Contact</a></li>
         <h3 class="text-muted">BORAJI.COM</h3>
      <div class="jumbotron">
         <h1>Using Webjars in Spring Boot application</h1>
         <div class="alert alert-success" role="alert"><b>Server Time is</b> ${dateTime}</div>

Application properties

Use the following Spring Boot properties in file  to resolve the JSP views.

spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix = .jsp

Controller class

Create a simple @Controller class under com.boraji.tutorial.springboot.controller package and write the following code in it.

package com.boraji.tutorial.springboot.controller;

import java.time.LocalDateTime;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

public class MyController {

  public String index(Model model) {
    return "index";


Run application

To bootstrap spring MVC application using Spring Boot, create a main class annotated with @SpringBootApplication annotation as follows.

package com.boraji.tutorial.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringBootApp {

  public static void main(String[] args) {, args);



Run the class as Java application i.e. go to Run→ Run as → Java Application

Or, you can use the mvn spring-boot:run command to run the spring boot application.

After executing the main class, enter the http://localhost:8080 in browser's address bar to see the bootstrap loaded from the Webjars.


Packaging and executing  Boot application as war

You can use the following maven command to package your spring boot application as war.

mvn clean package

Use the following command to run the war from the CMD.

>java -jar spring-boot-webjars-example-0.0.1-SNAPSHOT.war


Download Sources