Jetty4Spring

this library helps with embedding Jetty to Spring application

License

License

Categories

Categories

Ant Build Tools
GroupId

GroupId

org.urbantower
ArtifactId

ArtifactId

j4s
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Jetty4Spring
this library helps with embedding Jetty to Spring application
Project URL

Project URL

https://github.com/urbantower/jetty4spring/
Source Code Management

Source Code Management

https://github.com/urbantower/jetty4spring.git

Download j4s

How to add to project

<!-- https://jarcasting.com/artifacts/org.urbantower/j4s/ -->
<dependency>
    <groupId>org.urbantower</groupId>
    <artifactId>j4s</artifactId>
    <version>0.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.urbantower/j4s/
implementation 'org.urbantower:j4s:0.1.0'
// https://jarcasting.com/artifacts/org.urbantower/j4s/
implementation ("org.urbantower:j4s:0.1.0")
'org.urbantower:j4s:jar:0.1.0'
<dependency org="org.urbantower" name="j4s" rev="0.1.0">
  <artifact name="j4s" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.urbantower', module='j4s', version='0.1.0')
)
libraryDependencies += "org.urbantower" % "j4s" % "0.1.0"
[org.urbantower/j4s "0.1.0"]

Dependencies

compile (7)

Group / Artifact Type Version
org.springframework : spring-context jar 4.1.6.RELEASE
org.springframework : spring-web jar 4.1.6.RELEASE
org.springframework : spring-webmvc jar 4.1.6.RELEASE
org.eclipse.jetty : jetty-server jar 9.1.3.v20140225
org.eclipse.jetty : jetty-servlet jar 9.1.3.v20140225
org.eclipse.jetty : jetty-webapp jar 9.1.3.v20140225
org.apache.httpcomponents : httpclient jar 4.5.1

test (4)

Group / Artifact Type Version
org.testng : testng jar 6.9.6
org.springframework : spring-test jar 4.1.6.RELEASE
com.fasterxml.jackson.core : jackson-core jar 2.6.2
com.fasterxml.jackson.core : jackson-databind jar 2.6.2

Project Modules

There are no modules declared in this project.

Jetty4Spring

This library helps you with embedding Jetty to Spring application.

Motivation

Embedding web container (Jetty) into application is a new way how to create standalone and lightweight applications. When I decided embed the Jetty into one application (before amazing Spring Boot), I've bother with huge and hard-to-read XML. I decided to write a Spring namespace that simplify these XML fragments and make them more readable. This library is a result of this decision.

How to use

If you're using Maven, add dependencies below into your project.

 <dependency>
        <groupId>org.urbantower</groupId>
        <artifactId>j4s</artifactId>
        <version>0.1.0</version>
 </dependency>

Then you have to add http://www.urbantower.io/schema/jetty XML namespace into your Spring XML and you can define Jetty server:

  <?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:jetty="http://www.urbantower.io/schema/jetty"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                             http://www.urbantower.io/schema/jetty http://www.urbantower.io/schema/jetty/jetty.xsd">           
      <jetty:server http-port="9090">
      </jetty:server>          
  </beans>

When you run your spring application with this XML and type localhost:9090 into your browser, you should see No context on this server matched or handled this request. message powered by Jetty.

Registering servlets

To produce a response to a request, Jetty requires a handler. Jetty4Spring support you with several handlers in XML. Everything what you have to do is use the servlet-context tag that prepare servlet context handler.

<jetty:server http-port="9090">
    <jetty:servlet-context context-path="/webapp">
        <jetty:servlet url-pattern="/servlet" class="org.urbantower.j4s.example.HelloWorldServlet" />
    </jetty:servlet-context>          
</jetty:server>               

When you type into your browser http://localhost:9090/webapp/servlet, then you should see HelloWorldServlet's response.
Note: you have to ensure the spring-web dependency in your project.

Registering Spring's DispatcherServlet

You can easily use Spring DispatcherServlet servlet with current application context as a root context inside MessageDispatcher's context.

<jetty:server http-port="9090">
    <jetty:servlet-context context-path="/webapp">
        <jetty:servlet-dispatcher context-config-location="classpath:/spring-mvc/rest.xml" />
    </jetty:servlet-context>
</jetty:server>

The dispatcher servlet creates sub-context from rest.xml and servlet is binded to http://localhost:9090/webapp/*. Be carefull with url-pattern in servlet-dispatcher. In this case you have to be familiar with alwaysUseFullPath otherwise URL mapping will fail in any case.

Using context handler collections

You might have multiple handlers for your server. Jetty for that purpose is using HandlerCollection. This collection is representing by <handlers> tag in XML. See example below:

<jetty:server http-port="9090">
    <jetty:handlers>
        <jetty:servlet-context context-path="/module1">
            ...
        </jetty:servlet-context>
        <jetty:servlet-context context-path="/module2">
            ...
        </jetty:servlet-context>
        <jetty:default-handler/>
    </jetty:handlers>
</jetty:server>               

There is also another kind of collection, that will look for all Handler implementations in your app. context and create collection:

<jetty:server http-port="9090">
       <jetty:handlers>
            <jetty:autowired-handlers/>
            <jetty:default-handler/>
        </jetty:handlers>
</jetty:server>
   
<jetty:servlet-context context-path="/module1">
    ...
</jetty:servlet-context>

<jetty:servlet-context context-path="/module2">
    ...
</jetty:servlet-context>

Troubleshooting

Have a question or new requirement? Need a help? Create an issue.

Licencing

Copyright (C) 2011 Zdenko Vrabel Licensed under the Apache License, Version 2.0

org.urbantower

UrbanTower

We are nonprofit group of enthusiastic developers from Kosice/Slovakia

Versions

Version
0.1.0