Request not addressed by spring controller - spring

I have configured in following way that spring MVC app using Spring 3.1.1.RELEASE
web.xml
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context/*-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/cgi/*</url-pattern>
</servlet-mapping>
springmvc-servlet.xml
<context:component-scan base-package="com.pokuri.mvc.controllers"/>
UserController.java
package com.pokuri.mvc.controllers;
#Controller
#RequestMapping("/user")
public class UserController {
#RequestMapping(method=RequestMethod.GET)
#ResponseBody
public String defaultRequest(){
return "It's a default handler method";
}
}
index.jsp
<a class="action" href="cgi/user">Default Action</a>
Sever log on initializing springmvc app:
23:53:04,406 INFO [DispatcherServlet] FrameworkServlet 'springmvc': initialization started
23:53:04,468 INFO [XmlWebApplicationContext] Refreshing WebApplicationContext for namespace 'springmvc-servlet': startup date [Thu Jun 28 23:53:04 IST 2012]; root of context hierarchy
23:53:04,609 INFO [XmlBeanDefinitionReader] Loading XML bean definitions from ServletContext resource [/WEB-INF/context/services-context.xml]
23:53:04,890 INFO [DefaultListableBeanFactory] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#6c4fe: defining beans [dummyService]; root of factory hierarchy
23:53:05,156 INFO [DispatcherServlet] FrameworkServlet 'springmvc': initialization completed in 750 ms
.....
.....
23:56:17,875 WARN [PageNotFound] No mapping found for HTTP request with URI [/springmvc/cgi/user] in DispatcherServlet with name 'springmvc'
When I click on that link in index.jsp, I am getting 404 error. I thing I might have done a silly mistake. But, not able to track it. Can someone help me in this?

It looks like your DispatcherServlet is looking for config files ending in -context.xml. But your Spring config file is called springmvc-servlet.xml.

Related

sockjs info 404 error

i use tomcat 7.0.77, spring, now sockjs can not open info.
web.xml
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/cmd/*</url-pattern>
<url-pattern>/webSocketServer</url-pattern>
<url-pattern>/sockjs/webSocketServer/*</url-pattern>
<url-pattern>/sockjs/webSocketServer/info</url-pattern>
<url-pattern>/webSocketServerLogout</url-pattern>
<url-pattern>/webSocketServerSpAlarm</url-pattern>
java
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {
#Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
// registry.addHandler(systemWebSocketHandler(),"/webSocketServer").addInterceptors(new WebSocketHandshakeInterceptor());
registry.addHandler(systemWebSocketHandler(), "/sockjsWebSocketServer").addInterceptors(new WebSocketHandshakeInterceptor())
.withSockJS();
catalina.out
INFO: Mapped URL path [/sockjs/webSocketServer/**] onto handler of type [class org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler]
Jul 17, 2017 1:01:46 PM org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler
html
websocket = new SockJS("http://"+local+"/sockjs/webSocketServer");
firstly can not open /sockjs/webSocketServer/info when i add /sockjs/webSocketServer/info into web.xml like above, report
http://172.24.170.178/sockjs/webSocketServer/093/vl3vnd0p/websocket 404 error
thanks
add servlet-mapping /sockjs/* to web.xml
and change html to
websocket = new SockJS("http://"+local+"/sockjs/sockjsWebSocketServer");
ok now.
because for sockjs, in webConfig java map url can not directly put it to web.xml.
need add a prefix sockjs(example).

404 Error from REST Endpoint in SpringMVC for DispatcherServlet

RESOLVED PER THIS ANSWER
Spring invokes wrong controller mapping
Spring #Controllers URLs are always interpreted relative the the Spring Dispatcher Servlet that handles them. So if you map the dispatcher servlet to /api/ in web.xml then the URL to your controller above is /api/api/choice
The double string service/service/1234 was working.
ORIGINAL POST
Accessing a REST resource endpoint gives me a 404 error although everything seems to be defined correctly:
Log output:
DEBUG DispatcherServlet with name 'mvc-dispatcher' processing GET request for [/myapp/service/1234]
DEBUG Looking up handler method for path /1234
DEBUG Did not find handler method for [/1234]
WARN No mapping found for HTTP request with URI [/myapp/service/1234] in DispatcherServlet with name 'mvc-dispatcher'
web.xml
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
SpringMVC Controller
#RestController
#RequestMapping("/service")
public class RESTController {
#RequestMapping(value="/{id}", method=RequestMethod.GET)
public String getResult ( #PathVariable String id )
{
//... get JSON result
}
}
Expected invocation: myapp/service/1234
Also tried these options:
1) Don't define a class RequestMapping, just do a Method Request Mapping
#RequestMapping("/service/{id}")
2) as well as
#RequestMapping("/service*/{id}")
#RequestMapping("/service**/{id}")
Keep getting a 404 with the log above.
update your web.xml file :
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

Mapping issues with Spring-webmvc [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 5 years ago.
In setting up a Spring app (packaged as a WAR and hosted via Tomcat) I'm getting a 404 and the following error when I try to visit 'localhost:8080':
4479 [http-bio-8080-exec-1] WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/WEB-INF/pages/index.html] in DispatcherServlet with name 'spring'
There is most definitely a /WEB-INF/pages/index.html file.
Here's my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Additionally, here's my spring-servlet.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.company.app.controller" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value= "/WEB-INF/pages/"/>
<property name="suffix" value=".html"/>
</bean>
</beans>
And the get method from my controller:
package com.company.app.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class SplashController {
#RequestMapping(value="/", method=RequestMethod.GET)
public String index() {
return "index";
}
}
So, what's wrong with my configuration?
Additionally: I'd like to include my console output from Spring's initial setup, maybe it will provide a clue or two as to what is missing:
0 [localhost-startStop-1] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
92 [localhost-startStop-1] INFO org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Sun Jan 26 16:16:57 EST 2014]; root of context hierarchy
147 [localhost-startStop-1] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-servlet.xml]
776 [localhost-startStop-1] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.company.app.controller.SplashController.index()
1547 [localhost-startStop-1] INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Root mapping to handler 'splashController'
1771 [localhost-startStop-1] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 1770 ms
Jan 26, 2014 4:16:59 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'spring'
1809 [localhost-startStop-1] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'spring': initialization started
1813 [localhost-startStop-1] INFO org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'spring-servlet': startup date [Sun Jan 26 16:16:59 EST 2014]; parent: Root WebApplicationContext
1817 [localhost-startStop-1] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-servlet.xml]
1947 [localhost-startStop-1] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.company.app.controller.SplashController.index()
2164 [localhost-startStop-1] INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Root mapping to handler 'splashController'
2205 [localhost-startStop-1] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'spring': initialization completed in 396 ms
For whatever reason, changing the servlet-mapping portion of my web.xml to this solved the problem:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
I use Spring 3.2.3 release. Try this configuration:
Correct web.xml:
<servlet-mapping>
<servlet-name>Spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Correct Spring-servlet.xml:
<context:component-scan base-package="your.package.controller"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean id="internalViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp"/>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
Correct mapping annotacion:
#RequestMapping(value="/index", method=RequestMethod.GET)
And create index.jsp file.
I also faced the same problem when a friend of mine suggest me to check for the package declaration.
After I knew for which package the definition of the controller request mapping and context:component-scan base-package to check for I saw it was not the same and after giving it the proper package name it started to work.
Check for the following snippet in your Spring config file
<context:component-scan base-package="your package name" >

Spring methods being called twice

I have a Spring, PrimeFaces JSF application configured to load an application through a class that implements WebApplicationInitializer rather than web.xml (I do still have a bare web.xml) though. The problem is on application start and basically throughout the whole application, methods are being called twice! Best explanation I found is possible double loading of one of the listeners. I don't see I'm doing that. Attached is my WebApplicationInitializer class below. I'm not sure what to else to provide to resolve my issue. Even managed bean methods are being called twice.
public class WebAppInitializer implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
final CharacterEncodingFilter cf = new CharacterEncodingFilter();
cf.setEncoding("UTF-8");
cf.setForceEncoding(true);
servletContext.addListener(new RequestContextListener());
servletContext
.addFilter(
"ShiroFilter",
org.apache.shiro.web.servlet.IniShiroFilter.class)
.addMappingForUrlPatterns(null, false, "/*");
final WebApplicationContext context = getContext();
servletContext.addListener(new ContextLoaderListener(context));
final ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("*.do");
}
private AnnotationConfigWebApplicationContext getContext() {
final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(AppConfig.class);
return context;
}
}
My web.xml has the standard definitions for faces and the following mapping:
.....
<context-param>
<param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
<param-value>true</param-value>
</context-param>
.....
<!-- JSF Mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
When the application starts up, I see output from Spring that is also duplicated!
2013-12-12T11:29:56.430-0500|INFO: [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] - Refreshing Root WebApplicationContext: startup date [Thu Dec 12 11:29:56 EST 2013]; root of context hierarchy
2013-12-12T11:29:56.430-0500|INFO: [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] - Refreshing Root WebApplicationContext: startup date [Thu Dec 12 11:29:56 EST 2013]; root of context hierarchy
2013-12-12T11:29:56.978-0500|INFO: [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] - JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
2013-12-12T11:29:56.978-0500|INFO: [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] - JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
How do I stop the duplicate calls? Thanks.
EDIT
I'm using Log4j. Here's my configuration:
log4j.rootLogger=info, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%c] %x - %m%n
log4j.logger.org.hibernate=info, A1
log4j.logger.com.telus=info, A1
log4j.logger.org.springframework=info, A1
Using Spring 3.1.4.RELEASE. PrimeFaces 4.0. JSF 2.1.7. Deployed on Glassfish 3. Java 1.6.32.
Try adding the following to your log4j.properties
log4j.additivity.org.hibernate=false
log4j.additivity.com.telus=false
log4j.additivity.org.springframework=false
That should ensure that log messages aren't duplicated by the parent root logger.

SpringMVC: DispatcherServlet makes extra requests to view

I'm having a strange problem with Spring MVC. I have a simple controller like this:
#Controller
#RequestMapping("admin")
public class AdminController {
#RequestMapping(value = "", method = RequestMethod.GET)
public String home() {
return "home";
}
When I run my server and access the url: localhost/admin I get a 404 error. The view home.jsp exists and should be rendered. When I check my spring event log this is what shows up:
DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'appServlet' processing GET request for [/admin]
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /admin
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public java.lang.String be.roots.buildinginspector.web.controller.AdminController.home()]
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'adminController'
DEBUG: org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/admin] is: -1
DEBUG: org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'home'; URL [home]] in DispatcherServlet with name 'appServlet'
DEBUG: org.springframework.web.servlet.view.JstlView - Added model object 'domainOfExpertise' of type [be.roots.buildinginspector.business.model.DomainOfExpertise] to request in view with name 'home'
DEBUG: org.springframework.web.servlet.view.JstlView - Added model object 'org.springframework.validation.BindingResult.domainOfExpertise' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'home'
DEBUG: org.springframework.web.servlet.view.JstlView - Forwarding to resource [home] in InternalResourceView 'home'
DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'appServlet' processing GET request for [/home]
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /home
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/home]
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/home] in DispatcherServlet with name 'appServlet'
Everything is handled correctly but instead of just showing the view, the DispatcherServlet makes a new GET request to the url of the requested view name.
My web.xml:
<?xml version="1.0" encoding="UTF-8"?>
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/config-core-business.xml
classpath*:/spring/config-app-security.xml
</param-value>
</context-param>
<!-- Spring Security filter -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/appServlet/config-core-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Relevant spring context parts (config-core-web.xml):
<resources mapping="/resources/**" location="../../../resources" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/"/>
<beans:property name="suffix" value=".jsp"/>
</beans:bean>
#Controller
#RequestMapping("admin")
public class AdminController {
#RequestMapping(method = RequestMethod.GET)
public String home() {
return "home";
}
remove 'value' property of #RequestMapping for home() function.
After all it turned out this was a tomcat-related issue. For some reason when I recreated the configuration in my IDE the error was resolved. Thanks for your help.
I think this is servlet mapping problem in web.xml. Change it in web.xml to /admin addreses only. Perhaps now you have:
<url-pattern>*</url-pattern>
change it to:
<url-pattern>/admin/*</url-pattern>
The request mapping annotation defined over your methode restrict your controller to responds to requests starting with "/admin/home".
I would apply the following modifications :
#Controller
#RequestMapping("/admin")
public class AdminController {
#RequestMapping(method = RequestMethod.GET)
public String home() {
return "home";
}
}
Try this:
#RequestMapping(value = "admin",
method = {RequestMethod.GET, RequestMethod.POST })

Resources