Exception running a simple websocket with Camel component on Jetty - websocket

I'm trying to run use Camel Websocket component on Jetty but cannot get rid of the following exception. I'm not sure if this a issue with Jetty running with Camel or issues with versions compatibility or something is missing in my code.
I've put the project on Github in case someone wants to give it a try.
https://github.com/soumyasd/jettycamelwebsocket
Following are the steps to run the application.
Update your Twitter credentials in this class src/main/java/demo/websocket/
$mvn clean install
$mvn jetty:run
Point your web browser (I used Google Chrome) to http://localhost:8080/index.html
java.lang.NullPointerException
at org.eclipse.jetty.websocket.WebSocketFactory.upgrade(WebSocketFactory.java:236)[jetty-websocket-8.1.9.v20130131.jar:8.1.9.v20130131]
at org.eclipse.jetty.websocket.WebSocketFactory.acceptWebSocket(WebSocketFactory.java:382)[jetty-websocket-8.1.9.v20130131.jar:8.1.9.v20130131]
at org.eclipse.jetty.websocket.WebSocketServlet.service(WebSocketServlet.java:104)[jetty-websocket-7.6.8.v20121106.jar:7.6.8.v20121106]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)[javax.servlet-3.0.0.v201112011016.jar:]
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:598)[jetty-servlet-8.1.3.v20120416.jar:8.1.9.v20130131]
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:486)[jetty-servlet-8.1.3.v20120416.jar:8.1.9.v20130131]
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1065)[jetty-server-8.1.3.v20120416.jar:8.1.9.v20130131]
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:413)[jetty-servlet-8.1.3.v20120416.jar:8.1.9.v20130131]
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:999)[jetty-server-8.1.3.v20120416.jar:8.1.9.v20130131]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)[jetty-server-8.1.3.v20120416.jar:8.1.9.v20130131]
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111)[jetty-server-8.1.3.v20120416.jar:8.1.9.v20130131]
at org.eclipse.jetty.server.Server.handle(Server.java:350)[jetty-server-8.1.3.v20120416.jar:8.1.9.v20130131]
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:454)[jetty-server-8.1.3.v20120416.jar:8.1.9.v20130131]
at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:890)[jetty-server-8.1.3.v20120416.jar:8.1.9.v20130131]
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:944)[jetty-server-8.1.3.v20120416.jar:8.1.9.v20130131]
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:630)[jetty-http-8.1.3.v20120416.jar:8.1.9.v20130131]
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:230)[jetty-http-8.1.3.v20120416.jar:8.1.9.v20130131]
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:77)[jetty-server-8.1.3.v20120416.jar:8.1.9.v20130131]
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:606)[jetty-io-8.1.3.v20120416.jar:8.1.9.v20130131]
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:46)[jetty-io-8.1.3.v20120416.jar:8.1.9.v20130131]
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:603)[jetty-util-8.1.3.v20120416.jar:8.1.9.v20130131]
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:538)[jetty-util-8.1.3.v20120416.jar:8.1.9.v20130131]
at java.lang.Thread.run(Thread.java:680)[:1.6.0_45]
Here is my main Camel route.
package demo.websocket;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.twitter.TwitterComponent;
import org.apache.camel.component.websocket.WebsocketComponent;
public class TwitterStreamRoute extends RouteBuilder{
//put your twitter keys here to test
public final String CONSUMER_KEY = "";
public final String CONSUMER_SECRET = "";
public final String ACCESS_TOKEN = "";
public final String ACCESS_TOKEN_SECRET = "";
#Override
public void configure() throws Exception {
TwitterComponent tc = getContext().getComponent("twitter", TwitterComponent.class);
tc.setAccessToken(ACCESS_TOKEN);
tc.setAccessTokenSecret(ACCESS_TOKEN_SECRET);
tc.setConsumerKey(CONSUMER_KEY);
tc.setConsumerSecret(CONSUMER_SECRET);
fromF("twitter://streaming/filter?type=polling&delay=%s&keywords=%s", "5", "pittsburgh")
.process(new Processor() {
#Override
public void process(Exchange exchange) throws Exception {
String res = exchange.getIn().getBody().toString();
exchange.getOut().setBody(res);
}
})
.to("websocket://0.0.0.0:9292/camel-tweet?sendToAll=true");
}
}
The Camel config looks like the following:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<!-- Here we define Camel, notice the namespace it uses -->
<camelContext xmlns="http://camel.apache.org/schema/spring" trace="true">
<routeBuilder ref="twitter-route-id" ></routeBuilder>
</camelContext>
<bean id="twitter-route-id" class="demo.websocket.TwitterStreamRoute" />
</beans>
My web.xml looks like
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:camel="http://camel.apache.org/schema/spring"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
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">
<!-- your web.xml content here -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:camel-config.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<display-name>Archetype Created Web Application</display-name>
</web-app>
~
I'm using the following versions in my pom.xml. The full pom.xml is here.
<properties>
<spring.version>3.2.2.RELEASE</spring.version>
<camel.version>2.11.0</camel.version>
<jetty.version>8.1.3.v20120416</jetty.version>
</properties>
UPDATE (6-May-2013, 3:04 PM EST)
I updated the pom.xml to use Jetty 7.6.x release as suggested by Claus Ibsen.
But I'm still getting the same error. Also in this case the stackstrace have the same version numbers for WebSocketFactory and WebSocketServlet.
java.lang.NullPointerException
at org.eclipse.jetty.websocket.WebSocketFactory.upgrade(WebSocketFactory.java:236)[jetty-websocket-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.websocket.WebSocketFactory.acceptWebSocket(WebSocketFactory.java:382)[jetty-websocket-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.websocket.WebSocketServlet.service(WebSocketServlet.java:104)[jetty-websocket-7.6.8.v20121106.jar:7.6.8.v20121106]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)[javax.servlet-2.5.0.v201103041518.jar:]
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:652)[jetty-servlet-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:447)[jetty-servlet-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1038)[jetty-server-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:374)[jetty-servlet-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:972)[jetty-server-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)[jetty-server-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)[jetty-server-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.server.Server.handle(Server.java:363)[jetty-server-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:483)[jetty-server-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:920)[jetty-server-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:982)[jetty-server-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:635)[jetty-http-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)[jetty-http-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)[jetty-server-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)[jetty-io-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)[jetty-io-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)[jetty-util-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)[jetty-util-7.6.8.v20121106.jar:7.6.8.v20121106]
at java.lang.Thread.run(Thread.java:662)[:1.6.0_32]
UPDATE 2 (6-May-2013 4:06 PM EST)
As suggested by #JoakimErdfelt in the comments the Google Chrome file with request response information is here.

I don't think I've figured out the exact reason behind this error. However, I've a solution that works without major changes to the code.
I believe the problem is with the classloading for websocket related classes in Jetty and the Jetty-maven-plugin. Either my plugin in my pom.xml is not configured properly or I'm missing some entries. In any case, after trying this out for almost a day my alternative solution is as follows:
Instead of using the Jetty-maven-plugin to deploy the application I used Jetty Runner (http://wiki.eclipse.org/Jetty/Howto/Using_Jetty_Runner) to deploy the application. NOTE: Please download the version that is matches your jetty.version in your pom.xml.
For example:
$java -jar jetty-runner-7.6.8.v20121106.jar target/jettycamelwebsocket.war
After this I was able to access the websocket from my index.html without any issues.
Now if someone could explain why this doesn't work (or what's missing) from the Jetty-maven-plugin it would be really useful.
Here are a few references that I found useful. Although they are dated some of them gave me a hint that there maybe something wrong with the Jetty Maven Plugin, therefore pointing me the correct direction.
http://dev.eclipse.org/mhonarc/lists/jetty-users/msg00263.html

I faced the same exception couple of days back. The issue is null Connection object. At least for me the root cause was jetty settings. Make sure you have websocket enabled in your jetty start.ini file. Something like this -
OPTIONS=Server,jsp,resources,ext,plus,websocket
It should be enabled by default, but it wasn't in our case since somebody customized our start.ini and removed websocket support by mistake. After adding it back websocket worked like charm with Jetty 7.6.8.

The NPE on WebSocketFactory:236 is due to an attempt to upgrade without arriving in via a real Jetty HTTP connection.
The most common causes for this:
Attempting to use Jetty WebSockets on a web container that isn't jetty. (such as tomcat or jboss)
Attempting to unit test your websocket (servlet) without using a real HTTP connection (such as via a mocking library)

Try use Jetty 7.6.x as thats the version we test and used for the Apache Camel 2.11 release.

Related

Deploying a RAP .war file in jetty

I created an hello-world RAP application following the eclipse tutorial. I have no problems starting it in eclipse.
Now i want to package it as a .war file with maven and deploy it inside a jetty server. That's the point where i'm already unsure if it is the right approach.
My .war file includes all RAP, equinox and maven plugins, a web.xml and a config.ini. I'm building it with Tycho but i'm open to other solutions.
config.ini:
#Product Runtime Configuration File
osgi.bundles=RapHello/BasicEntryPoint#start,\
org.eclipse.core.commands#start,\
org.eclipse.core.jobs#start,\
org.eclipse.equinox.common#start,\
org.eclipse.equinox.console#start,\
org.eclipse.equinox.ds#start,\
org.eclipse.equinox.http.registry#start,\
org.eclipse.equinox.servletbridge.extensionbundle,\
org.eclipse.equinox.http.servlet#start,\
org.eclipse.equinox.http.servletbridge#start,\
org.eclipse.equinox.registry#start,\
org.eclipse.equinox.servletbridge#start,\
org.eclipse.equinox.util#start,\
org.eclipse.osgi.services#start,\
org.eclipse.rap.jface#start,\
org.eclipse.rap.rwt#start,\
org.eclipse.rap.rwt.osgi#start,\
raphello.BasicEntryPoint#start
osgi.bundles.defaultStartLevel=4
web.xml (copied from another sample .war):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app id="WebApp">
<servlet id="bridge">
<servlet-name>equinoxbridgeservlet</servlet-name>
<display-name>Equinox Bridge Servlet</display-name>
<description>Equinox Bridge Servlet</description>
<servlet-class>org.eclipse.equinox.servletbridge.BridgeServlet</servlet-class>
<!-- Framework Controls could be useful for testing purpose, but
we disable it per default -->
<init-param>
<param-name>enableFrameworkControls</param-name>
<param-value>false</param-value>
</init-param>
<!-- Enable multi-language support for the extension registry -->
<!-- the OSGi console is useful for trouble shooting but will fill up your
appserver log quickly, so deactivate on production use. Uncomment
the -console parameter to enabled OSGi console access. -->
<init-param>
<param-name>commandline</param-name>
<param-value>-registryMultiLanguage <!-- -console --></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>equinoxbridgeservlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
When i deploy the file in my jetty server, i get no errors and it seems like the file gets picked up, but all i get are
HTTP ERROR 404
Problem accessing /hellorap. Reason:
ProxyServlet: /hellorap
i think i tried all possible paths. What i read so far is, that the path should be the file name of my .war but that doesn't work.
jetty start log:
INFO::main: Logging initialized #276ms to org.eclipse.jetty.util.log.StdErrLog
INFO:oejs.Server:main: jetty-9.4.10.v20180503; built: 2018-05-03T15:56:21.710Z; git: daa59876e6f384329b122929e70a80934569428c; jvm 1.8.0_181-b13
INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet
INFO:oejs.session:main: DefaultSessionIdManager workerName=node0
INFO:oejs.session:main: No SessionScavenger set, using defaults
INFO:oejs.session:main: node0 Scavenging every 600000ms
INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext#561b6512{/,file:///C:/Users/USR/AppData/Local/Temp/jetty-0.0.0.0-8081-raphello.war-_-any-511053963228532950.dir/webapp/,AVAILABLE}{webapps/raphello.war}
INFO:oejs.AbstractConnector:main: Started ServerConnector#2898ac89{HTTP/1.1,[http/1.1]}{0.0.0.0:8081}
INFO:oejs.Server:main: Started #4477ms
So the question is: What am i doing wrong? Is it even the right approach?
This answer doesn't seem to work so my error is propably at an earlier stage.

spring boot application deployment on weblogic throws 404 error

I have done below configurations and tried almost all solutions found but nothing helped. When i am deploying spring boot app in war package. no error got logged in weblogic log but the application throwing 404 error.
web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:root-context.xml</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</context-param>
<context-param>
<param-name>spring.liveBeansView.mbeanDomain</param-name>
<param-value>dev</param-value>
</context-param>
weblogic.xml
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:wls="http://www.bea.com/ns/weblogic/90"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://www.bea.com/ns/weblogic/90 http://www.bea.com/ns/weblogic/90/weblogic-web-app.xsd">
<wls:weblogic-version>12.1.2.0.0</wls:weblogic-version>
<wls:context-root>/services/userModule/</wls:context-root>
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>com.fasterxml</wls:package-name>
<wls:package-name>org.slf4j</wls:package-name>
<wls:package-name>org.springframework.*</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>
</wls:weblogic-web-app>
application.properties
spring.profiles.default=default
spring.profiles.active=default
spring.liveBeansView.mbeanDomain=default
cms.config.monitor.dir=/server/location/application/artifacts
application.messages.file.name=application-messages
application.config.file.name=application-config
root-context.xml
it contains application specific configurations.
ApplicationBegin.java
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class})
public class ApplicationBegin extends SpringBootServletInitializer implements WebApplicationInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ApplicationBegin.class);
}
public static void main(String[] args){
SpringApplication.run(ApplicationBegin.class, args);
}
Cannot exclude tomcat server from pom.xml as it is failing the compilation. is there a way to set tomcat as provided while using spring boot starter web?
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<!-- THIS WILL BE EXCLUDE ONLY FOR WEBLOGIC DEPLOYMENT -->
<!-- <exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion> -->
</exclusions>
</dependency>
The problem is the application runs fine with embedded tomcat but it is not working even not throwing any error when deploying on weblogic. Where should i look?
Can you try class loading as the parent last? So Spring boot will use it's own container libraries.
After trying the solutions i found from different people , it couldn't solve my issue and somehow i have solved it now. all the similar issues i have seen on this topic and the soultions, i finally understood no answer was actually a solution because in most of the cases the issue happens because of wrong configuration that weblogic dosen't understand. the worst part being it doesn't even throw error. In my case other than application.properties file and a root-context.xml, i explicitly specified web.xml file in /WEB-INF location and defined context-config location there. As soon as i removed the web.xml and refactored/filtered the project dependencies from top to bottom , it resolved the issue.
and also then i realized many handy solutions on the web for this issue wouldn't even be required if your configuration is correct. for an example, configuring a jpavendor won't require if you use spring boot jpa starter correctly.
so.. if you ever face this kind of deployment issue on weblogic , you may follow below steps -
Only deploy a bareminimum part of application and make it workable
on weblogic
then add your critical dependencies / configurations and deploy them one by one on weblogic and check if it is working
you should always run your boot application to other local server first for resolving major configuration issues ..tomcat is
good.
I had the same problem, but I finally managed to solve it.
The problem was the version of web.xml descriptor. If you put the web.xml file in your project with <web-app version="2.5">, even though your Weblogic supports servlet 3.0, the spring controllers would throw 404.
That also explains the behavior of your app - what's why it started working when you removed the web.xml file.

Deploying a camel project having no main method,web.xml

My project consists of a spring xml(having the camel context, weblogic configuration parameters, jms related configurations) a pojo having the route for camel (whose reference we provide in spring) and an xsl.
My project has to listen on a queue, transform the message and publish it onto another queue. Everything works fine until i try to deploy it. How do i go about deploying it...i do not have a main java class...i tested my code by invoking Main of org.apache.camel.spring.Main.
I have to deploy it onto weblogic since my route consumes from weblogic queue seamlessly. How do i do it?.jar or .war or .ear? And how do i go about creating my .jar or .war or .ear? Remember i have only 3 files in my project structure and a few dependent jars
You will deploy it as a war file. I have not done this with Weblogic but the steps for Tomcat are listed below:
First make sure that all the needed jars are packaged in your war file.
Bootsrap Spring to your WAR file by adding the following to your web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
Add location of your Spring XML file by using the following:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/camel-context.xml</param-value>
</context-param>
Package the war with Maven i.e. run the command mvn package
Deploy the war file to the server.

No mapping found for HTTP request with URI.... in DispatcherServlet with name [duplicate]

This question already has answers here:
Why does Spring MVC respond with a 404 and report "No mapping found for HTTP request with URI [...] in DispatcherServlet"?
(13 answers)
Closed 6 years ago.
I checked out nearly every relevant article on stackoverflow already, but I just cant fix my problem.
Here is the code:
web.xml:
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>/</url-pattern>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
spring-servlet.xml:
<context:component-scan base-package="com.mycompany.elso" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
myController:
public class myController {
#RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
return new ModelAndView("hello", "message", message);
}
}
Web Pages/index.jsp:
<html>
<head>
<title>Spring 3.0 MVC Series</title>
</head>
<body>
Say Hello
</body>
</html>
Web Pages/WEB-INF/jsp/hello.jsp:
<html>
<head>
<title>Spring 3.0 MVC Series: Hello World - ViralPatel.net</title>
</head>
<body>
${message}
</body>
</html>
So when i launch the appication the index.jsp is loaded correctly but when i click on the href to navigate to hello.jsp i got a 404 error and the server log says:
No mapping found for HTTP request with URI [/Elso/hello.html] in DispatcherServlet with name 'spring'
I've checked out dozens of articles like that, but I just can't find the mistake, anybody has any idea what could it be?
Add
<mvc:default-servlet-handler/>
to spring-servlet.xml
You could try and add an #Controller annotation on top of your myController Class and
try the following url /<webappname>/my/hello.html.
This is because org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping prepends /my to each RequestMapping in the myController class.
If you are using
<mvc:annotation-driven/>
make sure your spring-servlet.xml has correct
<context:component-scan base-package="com.....controller" /> tag.
Basically, you need to include all the packages where you have used the annotation in your java code.
Also, please ensure you do not have duplication of component-scan (for a discovery of beans). If your config XML already contains the element, then any of your Controller classes that are annotated with #ComponentScan(basePackages=... needs to be stripped of the said annotation.
I solved my issue with :
Java Build Path -> JRE system library - > Edit -> Alternate JRE -> -> Finish
As it was configured to JDK folder so it was giving Exception
Make sure
<mvc:annotation-driven/>
<context:component-scan base-package="com.hireartists.web.controllers"/>
points to proper package that contains controllers.
Please check your [PROJECT_NAME]\target\classes directory to see whether myController.class is generated or not.
If not, please check all your java source code whether there are any compilation errors.
If you are using Java code based on Spring MVC configuration then enable the DefaultServletHandlerConfigurer in the WebMvcConfigurerAdapter object.
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
Try:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Worked for me!
Check ur Bean xmlns..
I also had similar problem, but I resolved it by adding mvc xmlns.
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="net.viralpatel.spring3.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
addition of <mvc:annotation-driven/> worked for me.
Add it before line <context:component-scan ............/>
If you want to serve .html files, you must add this <mvc:default-servlet-handler /> in your spring config file. .html files are static.
Hope that this can help someone.
It is not finding the controllers, this is basic issues.
it can be due to following reasons.
A. inside WEB-INF folder you have file web.xml that refers to dispatcherServlet. Here it this case is mvc-config.xml
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
B. This mvc-config.xml file have namespaces and it has to scan the controllers.
<context:component-scan base-package="org.vimal.spring.controllers" />
<mvc:annotation-driven />
C. Check for the correctness of the package name where you have the controllers. It should work.
All Controllers must be Annotated with #Controller.
Had the exact same error and it took me a long time trying to understand it. It is most likely down to compilation errors, the java classes did not get published in your servlet. Please check this by going in the server that you are using \tmp1\wtpwebapps[PROJECT_NAME]\WEB-INF\classes\ and try to find you controller classes to see whether or not they have been published. If not you need to get to the bottom of any compilation errors.
If you are using maven as build tool for project , build your project properly,your changes in the code and xml files are not reflecting after compilations.
If you depend on Spring Social, check that you have configured a Web Controller bean:
import org.springframework.context.annotation.Bean;
import org.springframework.social.connect.web.ConnectController;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionRepository;
...
#Bean
public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) {
return new ConnectController(connectionFactoryLocator, connectionRepository);
}
if you are using maven then do run maven install command before you run your web app on a server as it will generate a class file for your controller and in my experience that is what your application has been missing.
I had the same issue and after lots of reserach I found the classes were not getting published in my target folder. So I had run the below two commands from cmd
mvn clean install
mvn package
Surprisingly I was able to access the page and error was gone. Same can be verified from target folder where you will be able to find the complied classes which were missing earlier.
Add #Controller to your controller or where ever you have the #RequestMapping for you json end point.
This worked for me while deploying a similar application.
If you are using Maven ,
Add these to your pom.xml
<dependency>
<groupid>javax.servlet</groupid>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
<dependency>
<groupid>taglibs</groupid>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<scope>runtime</scope>
I also faced the same issue, but after putting the namespace below, it works fine:
xmlns:mvc="http://www.springframework.org/schema/mvc"
Removing the Tomcat Server and adding new tomcat configuration in Eclipse resolved issue for me.
In pom.xml make sure packaging is set to war like <packaging>war</packaging> ,not to jar or any thing else.
What is /Elso?
You try:
#RequestMapping("/Elso")
public class myController {
#RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
return new ModelAndView("hello", "message", message);
}
}

JAX WS webservice does not take spring bean from applicationcontext, hence throws null pointer exception

Hi I have got the webservice up and running , i have used jax ws. I have used Spring to be able to use beans with Autowired and stuff that spring gives like property value injection in applicationContext.xml.
I have the below spring applicationcontext.xml entry:
<context:component-scan base-package="com.mybeans.service" />
<bean id="myProperty" class="com.mybeans.service.MyBeanProperty"
p:Size="BIG">
</bean>
In web service end point class , i have done:
#Autowired private MyBeanProperty myProperty;
And I have a method :
public String getSize() {
return myProperty.getSize();
}
Unfortunately when i invoke the method it does not get any value and throws nullpointerexception.
PS: I used soapUI to run the wsdl of the webservice and invoked the method.
Is the webservice runs before the beans get created by Spring??
To duffmo
Yes i used component scan in applicationContext. And i do have the context loader listener as below in web.xml. Please help me..
Here is my complete code explainaton with code
I am using JAX-WS and Spring and try to setup a few WebServices which need run on Tomcat 7.
I am using Maven as build tool therefore I just list my two dependencies here:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.1.3</version>
</dependency>
</dependencies>
my service classes are located in com.test.services and are named TestService & HelloWorldService and look as follows:
package com.test.services;
import javax.jws.WebMethod;
import javax.jws.WebService;
#WebService( name = "Test", serviceName = "TestService" )
public class TestService {
#WebMethod
public String getTest() {
return "Test";
}
}
this is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>toolbox</display-name>
<description>testing webservices</description>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>jaxws-servlet</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jaxws-servlet</servlet-name>
<url-pattern>/testservice</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jaxws-servlet</servlet-name>
<url-pattern>/helloworldservice</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>10</session-timeout>
</session-config>
</web-app>
and this is my sun-jaxws.xml:
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>
<endpoint
name="jaxws-servlet"
implementation="com.test.services.TestService"
url-pattern="/testservice"/>
<endpoint
name="jaxws-servlet"
implementation="com.test.services.HelloWorldService"
url-pattern="/helloworldservice" />
</endpoints>
This works great and I can access the services by pointing my browser to [url]http://localhost:8080/toolbox/testservice[/url] respectively [url]http://localhost:8080/toolbox/helloworldservice[/url].
However Spring support is obviously not activated.
I tried the following which just leaver the HelloWorldService available:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>toolbox</display-name>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
and applicationContext.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:component-scan base-package="com.test.services" />
<bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
<property name="baseAddress" value="http://localhost:8080/" />
</bean>
</beans>
furthermore I annotated both Service classes with #Service annotation. As I mentioned before, this only publishes the alphabetically first webservice, hence HelloWorldService.
Also it changes the URL, as the service is now available as [url]http://localhost:8080/[/url] rather than [url]http://localhost:8080/toolbox/helloworldservice[/url].
The logging of Tomcat shows, that the Spring Context loads both Classes as Spring beans.
Do you have any ideas or suggestions on how to enable Spring support while keeping both services available??
It is answered here. Ultimately nothing worked other than adding below code to service impl.
#PostConstruct
public void init() {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
No Need to use ApplicationContext as well, if you do the following things
Annotate your service class with #Service
Service class should extends "SpringBeanAutowiringSupport".
Please have a look at the following snippet.
#org.springframework.stereotype.Service
#javax.jws.WebService (endpointInterface="a.b.c.MyPort",
targetNamespace="http://a.b.co.in/Retail/MyService/V1",
serviceName="MyService",
portName="MyServicePort",
wsdlLocation="wsdl/MyService.wsdl")
public class MyServiceBindingImpl extends org.springframework.web.context.support.SpringBeanAutowiringSupport{
I got the same issue, to resolve it I started the jaxws listener after the spring listener:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
Hope it helps
Your TestService (which is annotated with #WebService) should extend "SpringBeanAutowiringSupport" class to kick start spring binding.
Please have a look at the duffmo mentioned points as well.
I think it's more likely that your Spring context has not be loaded and made available to the web service. How have you done that?
You should have a ContextLoaderListener configured in the web.xml for the WAR in which the web service is deployed. Did you tell it where to load the Spring context? Are you using component scan?
http://renidev.wordpress.com/2009/02/02/how-to-use-springs-context-component-scan-and-annotation/
You misses the configuration for webservice inject. So put more inside the web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
Please don't forget the order. Because you need to init the bean for the autowired field first
Thanks

Resources