RequestMappingHandlerMapping.getHandlerInternal:230 - Did not find handler method for - java

trying to make some spring example program - constantly getting the error - it happens that my controller cannot handle /hello request. Here is debug info from log4j.
13:50:58,502 {TRACE} DispatcherServlet.initContextHolders:1018 - Bound request context to thread: org.apache.catalina.connector.RequestFacade#636f2067
13:50:58,503 {DEBUG} DispatcherServlet.doService:823 - DispatcherServlet with name 'springtest' processing GET request for [/springtest_2/hello]
13:50:58,504 {TRACE} DispatcherServlet.getHandler:1088 - Testing handler map
[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#7bab2c3] in DispatcherServlet with name 'springtest'
13:50:58,504 {DEBUG} RequestMappingHandlerMapping.getHandlerInternal:220 - Looking
up handler method for path /hello
13:50:58,504 {DEBUG} RequestMappingHandlerMapping.getHandlerInternal:230 - Did not find handler method for [/hello]
13:50:58,504 {TRACE} DispatcherServlet.getHandler:1088 - Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping#722e242b] in DispatcherServlet with name 'springtest'
13:50:58,505 {TRACE} BeanNameUrlHandlerMapping.getHandlerInternal:127 - No handler mapping found for [/hello]
13:50:58,505 {WARN} PageNotFound.noHandlerFound:1108 - No mapping found for HTTP request with URI [/springtest_2/hello] in DispatcherServlet with name 'springtest'
13:50:58,505 {TRACE} DispatcherServlet.resetContextHolders:1028 - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade#636f2067
13:50:58,505 {DEBUG} DispatcherServlet.processRequest:966 - Successfully completed request
13:50:58,506 {TRACE} XmlWebApplicationContext.publishEvent:332 - Publishing event in WebApplicationContext for namespace 'springtest-servlet': ServletRequestHandledEvent: url=[/springtest_2/hello]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[springtest]; session=[null]; user=[null]; time=[9ms]; status=[OK]
web.xml from my project.
<?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:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.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">
<display-name>Spring3-Hibernate DEMO</display-name>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springtest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springtest-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springtest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
spring-test-servlet.xml - dispatcher file from my project
<?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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" >
<mvc:annotation-driven />
<context:component-scan base-package="com.denmroot.controller"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
ControllerMain.java - from my project
package com.denmroot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class ControllerMain {
#RequestMapping(value = "/hello", method = RequestMethod.GET)
public String helloworld (ModelMap model) {
model.addAttribute("message", "Hello World !!! Spring Output");
return "hello";
}
}

For me, I had a typo in the Spring config file which points to the package:
Was:
<context:component-scan base-package="com.something.web.controlers" />
Fixed with correct spelling:
<context:component-scan base-package="com.something.web.controllers" />

ControllerMain.java requires a #RequestMapping("/") statement after the #Controller.
Change:
#Controller
public class ControllerMain {
#RequestMapping(value = "/hello", method = RequestMethod.GET)
....
....
To:
#Controller
#RequestMapping("/")
public class ControllerMain {
#RequestMapping(value = "/hello", method = RequestMethod.GET)
....
....
....

Actually the log says everything:
Or change your url from client to just /hello path
Or map your #Controller with:
#Controller
#RequestMapping("/springtest_2")
public class ControllerMain {

#Controller
annotation is missing for your controller class.

Related

Spring - RestController annotation does not catch request

I don't understand why If I use RestController annotation to declare a class as service, like this:
#RestController("/registration")
public class RegistrationService {
#RequestMapping(value="/",
produces="application/json")
public String initializeSession(Model model){
return "{\"success\":1}";
}
}
when I do a request like
http://localhost:8080/SpringRest/registration/
I get an 404 status and in the console:
No mapping found for HTTP request with URI [/SpringRest/registration/] in DispatcherServlet with name 'dispatcherServlet'
Everything works If I change #RestController("/registration")
with
#Controller
#RequestMapping("/registration")
and add #ResponseBody above method declaration.
This is my configuration:
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>SpringRest</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
- Servlet that dispatches request to registered handlers (Controller implementations).
-->
<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>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
dispatcher
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven />
<!-- Uncomment and your base-package here: -->
<context:component-scan
base-package="it.reply.rest"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
The declaration of RestController looks like:
#Target(value=TYPE)
#Retention(value=RUNTIME)
#Documented
#Controller
#ResponseBody
public #interface RestController
It includes #Controller and #ResponseBody but not #RequestMapping. So you need to add it. So the following shall work:
#RestController
#RequestMapping("/registration")
The correct usage is:
#RestController
#RequestMapping("/registration")
The value of #RestController is the component name.
From RestController (Spring Framework 4.3.9.RELEASE API):
public abstract String value
The value may indicate a suggestion for a logical component name, to
be turned into a Spring bean in case of an autodetected component.
#RestController not including url mapping so you need to #Requestmapping as well like
#RestController
#RequestMapping("/registration")

Spring MVC - Invalid view being executed (violating specified request mapping)

I have been trying to debug it for like hours and even tried to create the same project over and again. I don't know whether it's the issue due to IntelliJ IDEA or something else. I tried to google but couldn't find any solution. I am making a simple spring mvc demo application in intelliJ. A default structure was provided by intelliJ and a view "index.jsp" in web directory.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>spring-mvc-demo</display-name>
<!-- Spring MVC Configs -->
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-mvc-demo-servlet.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.luv2code.springdemo" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
DebugController.java
package com.luv2code.springdemo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Created by gaurav.ahirwar on 29/05/17.
*/
#Controller
public class DebugController {
#RequestMapping(value="/", method = RequestMethod.GET)
public String debug() {
return "hello";
}
}
The problem is that whenever I run the project and hit localhost:8080/ then index.jsp is executed but according to my configuration and mapping hello.jsp should be loaded. It may be possible that I am doing some silly mistake but I have tried as much as I can and now really frustrated. Please help me out. Thanks.
localhost:8080
Try this mapping:
#RequestMapping(value = {"", "/", "index.jsp"}, method = RequestMethod.GET)
I guess localhost:8080 equals "" path, but not /

SpringMVC controller can not deal with the request

I follow the instruction from the book "Spring in action" in SprinvMVC chapter
When i run the tomcat server and try the demo it return error like:
HTTP Status 404 – Not Found
Type Status Report
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
enter image description here
I using macOS , tomcat9 , Intellij 2017.1 , jdk 1.9
and here is my code.
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spitter</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spitter</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
spitter.xml:
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--static resources getting-->
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources"/>
<!--use annotations to create the mapping between-->
<!-- url and class deal with request(Controller) -->
<mvc:annotation-driven/>
<!--scan the component and auto regist as bean-->
<context:component-scan base-package="com.springmvc"/>
<!--Use this bean to map the jsp file according to the name return by Controller-->
<!--It will automatically add the prefix and suffix to the name string-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
controller:
package com.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by xwh on 29/3/2017.
*/
#Controller
public class HomeController {
// public static final int DEAFAULT_SPITTLES_PER_PAGE = 25;
public HomeController() {
System.out.println("-------HomeController init-------");
}
#RequestMapping("/")
public String showHomePage() {
System.out.println("-------showHomePage Method show-------");
return "home";
}
}
And here is my directory pic.
enter image description here
your contextConfigLocation file is spitter-servlet.xml?
In your web.xml file, you defined the context config file is applicationContext.xml. It means your configs in spitter.xml is not used.
try change the contextConfigLocation to /WEB-INF/spitter-servlet.xml

Spring project in Eclipse - 404

I try to run the app on Tomcat but keep getting this error "The requested resource (/testapp/) is not available." - what might be wrong? I guess my XML setup is incorrect but not sure how to fix it.
dispatcher-servlet.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="controller" />
<mvc:view-controller path="/" view-name="HelloWorldPage" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
HelloWorldController.java - using Spring annotations.
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
#Controller
#RequestMapping("/welcome")
public class HelloWorldController {
#RequestMapping(method = RequestMethod.GET)
public ModelAndView helloWorld() {
ModelAndView model = new ModelAndView("HelloWorldPage");
model.addObject("msg", "hello world");
return model;
}
}
in your web.xml you map spring to *.htm but your controller is mapped to
#RequestMapping("/welcome")
you should map to
#RequestMapping("/welcome.htm")
In case you are not getting any exception on eclipse console, I think you are probably not hitting the right contextRoot of your project, mostly it's the exact name of your application, but sometimes it's different...
If you are not typing the app name wrongly try this.
Click on the project right mouse button click on properties.
Then search for Context.
you are going to find a section called ContextRoot
Use the string in that box to mount your Contextroot like this
localhost:8080/stringFound/
and hit enter in the browser.

Why spring warn message is" org.springframework.web.servlet.PageNotFound "?

I trying spring 3 mvc this package is
org.spring.test and code is
#Controller
#RequestMapping("/welcome")
public class WelcomeController {
private Logger logger = org.slf4j.LoggerFactory.getLogger(WelcomeController.class);
#RequestMapping(method = RequestMethod.GET)
public void welcome() {
logger.info("Welcome!");
}
#RequestMapping("test1")
public void test1() {
logger.info("test1!");
}
#RequestMapping("test2")
public void test2() {
logger.info("test2!");
}
}
web.xml is:
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
last mvc.xml is
<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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="org.spring.test" />
<!-- Configures support for #Controllers -->
<mvc:annotation-driven />
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
When I use "mvn jetty:run" command and write to http://localhost:8080/springdemo/welcome
is
"HTTP ERROR 404
Problem accessing /springdemo/WEB-INF/views/welcome.jsp. Reason:
NOT_FOUND"
Console message is :
INFO : org.spring.test.WelcomeController - Welcome!
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/springdemo/WEB-INF/views/welcome.jsp] in DispatcherServlet with name 'Spring MVC Dispatcher Servlet'
Why?
It looks like you are missing /WEB-INF/views/test.jsp. Is that true?
You are requesting /test URI, which is not mapped to your controller class (which is mapped to /welcome, I think). Spring uses InternalResourceViewResolver to serve your request. It takes prefix + "test" + suffix. That is how you end up with /WEB-INF/views/test.jsp.
Make sure you have that JSP or change your controller mappings.
Update:
Ok, you changed the question. But do you have /springdemo/WEB-INF/views/welcome.jsp ?

Categories