Maven ignores #WebServlet annotation - servlets

I had a Dynamic Web project which had a few #WebServlet annotations, all of which worked fine. I have since switched my Dynamic Web project to a Maven web project. Since doing so, I can't access any of my servlets by what I defined in my #WebServlet annotations. I should also note that some of my servlets are overlay from other Maven projects. Do I have to now define my servlet mappings in the web.xml file?
Thanks

The Maven story aside, the #WebServlet annotation was introduced in Servlet 3.0. If they are not initialized, then it simply means that either the web application descriptor (the web.xml) is not declared conform Servlet 3.0 or that the target container does not support Servlet 3.0.
You need to verify if your web.xml conforms Servlet 3.0. I.e., the <web-app> root declaration must match that:
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

Related

"Hystrix stream is not generating for my spring mvc application"

I have a simple Spring MVC application. For that application I have Implemented Circuit Breaker Pattern using #EnableCircuitBreaker Annotation. It is working fine. But While trying to generate the hystrix stream it showing 404 error. Can anyone please help me out this.
Hystrix stream (/hystrix.stream) is only enabled if you have spring boot actuator dependency. Please try to add the below dependency into your pom or gradle build file.
org.springframework.boot:spring-boot-starter-actuator
Updated # 2017/06/26
If your application is not based on spring boot, you need to configure HystrixMetricsStreamServlet by yourself. Because /hystrix.stream is auto-configured by spring cloud netflix and it is based on spring boot.
First, you need to add dependency of com.netflix.hystrix:hystrix-metrics-event-stream into your application.
Second, you should HystrixMetricsStreamServlet servlet into web.xml like below.
<servlet>
<description></description>
<display-name>HystrixMetricsStreamServlet</display-name>
<servlet-name>HystrixMetricsStreamServlet</servlet-name>
<servlet-class>com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HystrixMetricsStreamServlet</servlet-name>
<url-pattern>/hystrix.stream</url-pattern>
</servlet-mapping
If you find the details about how to configure HystrixMetricsStreamServlet in your web application here.

XML-based Spring MVC argument resolver

I'm trying to migrate a big web app from Spring 2.5 to Spring 4. The whole context and MVCs are being configured using XML files, not annotated. Does anybody know if it's possible to declare argument resolvers to MVC beans injected using XML files? When using annotated controllers, the solution is to inject argument resolvers:
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="com.company.CustomResolver" />
</mvc:argument-resolvers>
</mvc:annotation-driven>
But I can't implement this solution because, due to size and legacy reasons, I can't migrate the app whole set of MVCs to annotated controllers.

Router servlet for webservice project

In my recent project i started using maven , instead of depending on RAD to deploy and build ear, i have been coming across little things why to use , which one is best..
My question here is , below code i copied form web.xml and it us com.ibm.ws.websvcs.transport.http.WASAxis2Servlet servlet by ibm to route http request to web services, is there any servlet present from java that can replace the above one , we dont want our ear generation should be dependent on specific application server
<servlet>
<servlet-name>com.test.HelloWorld</servlet-name>
<servlet-class>com.ibm.ws.websvcs.transport.http.WASAxis2Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>com.test.HelloWorld</servlet-name>
<url-pattern>/HelloWorldService</url-pattern>
</servlet-mapping>
No.
The Servlet that you're seeing in the web.xml is an IBM-proprietary Servlet that routes HTTP requests to Web Services and, as far as I can tell, if you're running WAS 7.0 onwards you don't need it. You can simply use JAX-WS to annotate your Web Service class.
EDIT as per OP's comment
As you're using WAS 8.5, you have JavaEE 6.0 at your disposal. You don't need any router project anymore. IBM's proprietary router was needed in previous versions of WAS in order to route HTTP requests to Web Services and/or EJBs; however, with JavaEE 6.0, you can use JAX-WS in order to automatically intercept HTTP requests by Web Service classes as well as EJBs.
If you have a "router" project, you can safely throw it away.

Accessing other spring defined beans in the portlet spring application context (Spring Portlet MVC)

Context: Using maven 3,spring portlet mvc 3.1
Background:
I have built a portlet using spring portlet mvc 3.1.
This portlet uses the spring portlet mvc defined Dispatched and is defined accordingly in the -portlet.xml.
The dispatcher portlet is configured to pass requests to the myController (POJO with annotation of #Controller)
I also have a service project (jar) which defines a business service to be used by myController. This service has its own spring file and defines the bean 'myService'
I want to inject myService into myController using a predefined bean, so I have defined ContextLoaderListener in the web.xml of my portlet project
Problem:
I have tried both ways of trying to inject myService into myController i.e. using annotations and xml defined beans, but the portlet fails on deployment with an error that myService bean could not be found (or when using annotations no matching bean with class type found)
Note: I can see some logs on undeploy that the beans are available, but I think the issue is with PortletApplicationContext (as defined by -portlet.xml) is loading before the root Web ApplicationContext (as defined applicationContext through default usage in web.xml)
Note2: If I put the import for spring file defining 'myService' bean into the -portlet.xml, then it works.
Would appreciate any help in sorting this out.

How to initialize a web app?

My Web App will be deployed as a WAR package in a Jetty instance. It needs to perform a lot of caching before serving requests. How do I call the caching method before anything else? is the a static void main() in the web app standard?
A standard (old) way is to code a Servlet which takes care of initialization stuff in its init() method. You force it to be initialized at application start by adding a load-on-startup positive value in your web.xml
<servlet>
<servlet-name>myinit</servlet-name>
<servlet-class>com.example.MyInitServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
Today it's more usual to have a bean container like Spring, which takes care of this kind of things (instantiating services objects, preloading cacheable sharable data, etc).
Note: this recipe is for webapps in general, not specific to Jetty.

Resources