Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded Jetty and Spring Web MVC

Tags:

java

spring

jetty

For a pet project I would like to have an embedded Jetty run a Spring Web MVC app. I've used Spring in web containers (where it's easy to tell "where to start") and I've used embedded Jetty without Spring.

It feels a bit like the chicken or the egg problem if I want both to work together. What is the best way to organize the project? In other words, what shall I put in main()? Should it be a Spring app that happens to have Jetty as a bean (what about contexts then?)? Or should I start Jetty alone and plug Spring in via servlet listener? What are the caveats?

like image 703
Konrad Garus Avatar asked May 07 '11 20:05

Konrad Garus


People also ask

Is Jetty embedded in spring boot?

Spring Boot applications contain embedded servers and when we run them, they run inside the embedded servers. Tomcat is the default embedded server in Spring Boot. However, we may want to run our application in an embedded Jetty server.

What is embedded Jetty?

Put another way, running Jetty in embedded mode means putting an HTTP module into your application, rather than putting your application into an HTTP server.

Does spring use Jetty?

By default, Spring boot uses embedded tomcat server to run the application. At times, you may need to use jetty server in place of tomcat server. Spring Boot provides Tomcat and Jetty dependencies bundled together as separate starters to help make this process as easy as possible.


1 Answers

Jetty in a Spring container is used to start webapp, springified or not. The webapp and your webapp don't have the same Spring context without tricks.

So, you have to create a Jetty server in your main, add your webapp and start the server. The best way is using a web.xml like a common Java EE server, and add this descriptor to your Jetty server.

like image 160
Vincent Devillers Avatar answered Sep 21 '22 13:09

Vincent Devillers