Force Spring Boot to use servlet mapping in web.xml - spring-boot

I'm currently trying to shift my existing dynamic web project to Spring boot project and it uses web.xml for servlet mapping. I understand that spring would ignore the web.xml file, what should be the correct approach for spring to use the existing web.xml? And yes, I still need to stick to using web.xml for this project.
I'm kinda new to this, please guide me through! Thanks!

I suppose that you need to stick with a web.xml because your container uses an older version of Servlet than 3.0.
Spring Boot is built on Servlet 3.0. You have to update your main class to extend SpringBootServletInitializer and override configure method, which tells spring to use its Servlet 3.0 support. Embedded containers like Tomcat need Servlet 3.0, so if you want to start your project during the development process (including JUnit tests) in embedded containers, I think, from what I know, the only way is to rewrite your web.xml to Servlet 3.0 java config. But if you really want to deploy you app in an older container, you still can by using spring-boot-legacy module. It allows you to use web.xml for older containers; only thing you have to do is to add
org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener in your web.xml.
For more information about deploying war in an old container, take a look at Spring Boot's official documentation.
Spring Boot uses Servlet 3.0 APIs to initialize the ServletContext
(register Servlets etc.) so you can’t use the same application out of
the box in a Servlet 2.5 container. It is however possible to run a
Spring Boot application on an older container with some special tools.
If you include org.springframework.boot:spring-boot-legacy as a
dependency (maintained separately to the core of Spring Boot and
currently available at 1.0.2.RELEASE), all you should need to do is
create a web.xml and declare a context listener to create the
application context and your filters and servlets.

This is a very old question, but I'm currently on the same situation and I will share my experience.
I extended the SpringBootServletInitializer class to create my #SpringBootApplication, and it will INCLUDE your web.xml configuration by default. I'm still able to declare or edit existing servlet mapping, and it will be taken in account.

Related

Simple REST service with Spring framework not using Spring Boot

I want to start with the most simple Maven Project with a simple REST resource not using Spring Boot and generate a .war artifact that is deployed in a servlet container. I am using Eclipse IDE. So I would like to know what is the basic things in place needed to create such a Project.
I think I need at least this dependences:
Spring-core, Spring-mvc, Spring-web, Spring-context,
I also need the stuff with a class annotated with the #RestController annotation, with some method annotated with the #Requestmapping and so.
But whats the minimum content I should have in the WebContent directory and its subfolders META-INF, WEB-INF ... in order to the servlet container to know how to use the .war component? I dont want any HTML nor JSP pages.
In your WEB-INF folder, you will need a web.xml file. This is where you will configure your dispatcher servlet. This is the part of your application that receives requests and delegates them to the appropriate part of your application.
You will also need some sort of REST configuration file. You can define beans for Spring and component scan config.
A good explanation of this can be found here, https://www.programming-free.com/2014/01/spring-mvc-40-restful-web-services.html

Spring boot - unable to use web.xml with WAR deployment

I am not able to use web.xml with spring boot in war deployment.
I am aware that there are ways of converting web.xml using #Configuration, but I must stick with web.xml for now. I was following the official document, here: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html#howto-create-a-deployable-war-file-for-older-containers
I was able to deploy my application in WAR file, but it was not using web.xml.
Maybe I am not following the step 85.2 correctly, as I am not sure what it means by "load an applicationContext via a DispatcherServlet":
85.2 Create a deployable war file for older servlet containers
Older Servlet containers don’t have support for the
ServletContextInitializer bootstrap process used in Servlet 3.0. You
can still use Spring and Spring Boot in these containers but you are
going to need to add a web.xml to your application and configure it to
load an ApplicationContext via a DispatcherServlet.
Can you guide me or point me to any examples or working sample project using web.xml in spring boot? I am using weblogic server.

Migrating an existing spring jersey servlet2.5, Jersey2.3 webapp to springboot

Evaluating to port an existing mid to large sized multi module JEE application, this consists servlet 2.5, Jersey for rest and Spring 3.2.9 (JDK8, Maven 3. currently we build a war file and deploy it on to aws. My objective for this exercise is to be able to use spring-boot to create a self contained executable jar that can be installed on AWS and this would help us just run on cloud with java -jar
Is it possible to use spring boot considering the project is not using spring-web module, it is more traditional Servlet2.5 statically declaring all servlets and filters in web.xml
which approach is better, adding spring boot as a parent or dependency.
Any experience and info our senior spring community members have in this kind of migration.
This is absolutely possible to do even if you're not using Spring Web MVC.
You can use spring-boot-starter-parent like a BOM POM, so you can import its dependencies without having to change your POM's parent.
Take a look at the Spring Boot documentation for migrating a web.xml application to Spring Boot. The main points here for traditionally deployed servlets and filters are:
A #Bean of type Servlet or ServletRegistrationBean installs that bean
in the container as if it was a <servlet/> and <servlet-mapping/> in
web.xml.
A #Bean of type Filter or FilterRegistrationBean behaves
similarly (like a <filter/> and <filter-mapping/>.

Migrating Spring web application to Spring Boot

I have a web project, and I depoly it on tomcat easily. Infact I have a WebAppInitializer class that implements WebApplicationInitializer (this class it's really fat), as you know every application server that supports servlet 3.0, it can easily detect it and try to boot it. Now I wonder that it could be possible to use spring boot starter and without any further configuration, I pass my WebAppInitializer to it and spring boot based on my WebAppInitializer boots my project?
I just want to use the approach of spring-boot to deploy application on Tomcat and I don't want to use other spring-boot's facilities.
Yes, it's an old question. But I do not see an accepted answer and the one closest to a working one only has a link to an external resource. So here it is.
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-embedded-container-context-initializer
28.4.2 Servlet Context Initialization
Embedded servlet containers do not directly execute the Servlet 3.0+
javax.servlet.ServletContainerInitializer interface or Spring’s
org.springframework.web.WebApplicationInitializer interface. This is
an intentional design decision intended to reduce the risk that third
party libraries designed to run inside a war may break Spring Boot
applications.
If you need to perform servlet context initialization in a Spring Boot
application, you should register a bean that implements the
org.springframework.boot.web.servlet.ServletContextInitializer
interface. The single onStartup method provides access to the
ServletContext and, if necessary, can easily be used as an adapter to
an existing WebApplicationInitializer.
Scanning for Servlets, Filters, and listeners
When using an embedded
container, automatic registration of classes annotated with
#WebServlet, #WebFilter, and #WebListener can be enabled by using
#ServletComponentScan.
[Tip] #ServletComponentScan has no effect in a standalone container,
where the container’s built-in discovery mechanisms are used instead.
I've tried it. It works
In my use case I have project containing a few dozens of webapps, designed to run on Tomcat as WAR. Lots of logics was neatly crafted into WebApplicationInitializers and it seemed there should be an easier way to reuse all this. Adding implements ServletContextInitializer to those initializers and exposing them as beans through #Configuration classes lit my webservers up with SpringBoot's embedded Tomcat.
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file describes precisely how to do it
From the Spring Boot docs:
Servlet 3.0+ applications might translate pretty easily if they already use the Spring Servlet 3.0+ initializer support classes. Normally all the code from an existing WebApplicationInitializer can be moved into a SpringBootServletInitializer. If your existing application has more than one ApplicationContext (e.g. if it uses AbstractDispatcherServletInitializer) then you might be able to squash all your context sources into a single SpringApplication. The main complication you might encounter is if that doesn’t work and you need to maintain the context hierarchy. See the entry on building a hierarchy for examples. An existing parent context that contains web-specific features will usually need to be broken up so that all the ServletContextAware components are in the child context.
So yes, it's possible but you need to convert it to SpringBootServletInitializer, which seems to be quite similar.

Does Spring Framework come with its own beans?

This text is from the book called Core Java Server Faces:
It is a historical accident that there are two separate mechanisms,
CDI beans and JSF managed beans, for beans that can be used in JSF
pages. We suggest that you use CDI beans unless your application must
work on a plain servlet runner such as Tomcat. The source code for the
book comes in two versions, one with CDI beans (for Java EE 6
application servers) and one with JSF managed beans (for servlet
runners without CDI support).
My question is:
If I use the Spring Framework, and a Tomcat Server, will I need to use one of the beans mentioned above, or does Spring Framework come with its own bean implementations?
As far as I know, Spring Framework supports Dependency Injection. Does it support it if I run the application on Tomcat? Does it mean that I will be using neither the CDI beans nor the JSF Managed means mentioned in this book?
Thank you.
talking about container is more correct than bean implementation. Yes Spring comes with its own container. In fact you can see spring frameworks as a kind of alternative to the full Java EE stack.
Using Spring DI and CDI together has about no interest but you still can use JSF with spring on tomcat although if i would advise you to switch to a Java EE 6 web profile server instead of spring in this case.
Spring comes with is own view framework implementation named spring mvc.
All of this can run perfectly on any servlet container (jetty tomcat etc...) on condition that you provide associated dependencies ofc.

Resources