Spring - RestController annotation does not catch request - java

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")

Related

Spring MVC : How url mapping was done before use of #RequestMapping() in spring framework?

I am learning spring MVC nowadays for new project.There I came across #RequestMapping annotations for how requests are mapped with URL.
I want to understand that , How request and URL is mapped previously without use of this annotation?
How it is configured in XML files?
Controller code:
#Controller
public class HelloController {
#RequestMapping(value = "/greeting")
public String sayHello(Model model){
model.addAttribute("greeting","Helloworld");
return "hello";
}
}
servlet-config.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:osgi="http://www.springframework.org/schema/osgi" 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.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.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.xsd">
<bean name="/greeting.html" class="com.pj.controller.HelloController"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
<servlet>
<servlet-name>fitTrackerServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>fitTrackerServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Check this out: Difference between the annotations #GetMapping and #RequestMapping(method = RequestMethod.GET)
Nowadays,
RequestMapping is used to map the endpoint of your controller.
For the http verbs, there are specific mapping annotations like
#Getmapping,
#PostMapping,
#PutMapping...

Not able to access Spring controller

I am trying to create a Spring RESTful API very a basic application from scratch, but I am not able to access the controller. I could access JSP file but not controller. I have tried with to annotate with #RestController as well but it didn't work. I am running on Tomcat 8.
Error is:
The origin server did not find a current representation for the target
resource or is not willing to disclose that one exists. 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">
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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">
<mvc:annotation-driven />
<context:component-scan
base-package="com.controller />
<mvc:default-servlet-handler />
</beans>
My Controller is
#Controller
public class TransactionControllerImpl{
#Autowired
private TransactionService transactionService;
#RequestMapping(method = RequestMethod.GET, value="/transaction")
#ResponseBody
public String getTransactionList() {
try {
System.out.println("from controller");
return "HEllow rahul";//transactionService.getTransactionList();
}
catch (Exception e) {
return null;
}
}
If you are creating application context separately, you should provide context param and value as location of your context.xml file.
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
For your error the controller is not accessible,it might be due to :-
<context:component-scan
base-package="com.controller />
check if you have written correct base-package name or try using
<context:component-scan base-package="..package name..">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
Hope it helps.
Remove listener from web.xml because you have only one context xml. If you want to load multiple context xml add listener and context param.

getting 404 when consumiing spring controller service

I have created a spring application (Spring version 3.2.8). The application is working fine but the issue which I am facing is that when I try to consume the controller service through url I am getting 404 error
The url which I try to consume through browser url is given below, which I expect to return a test string
http://localhost/Spring3Sample/user/getPersonDetails
My code is as given below
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/" p:suffix=".jsp" />
</beans>
applicationContext.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"
xsi:schemaLocation="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">
<context:component-scan base-package="com.controllers" />
</beans>
UserController.java
package com.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
#RequestMapping("/user")
public class UserController {
private static final String APP_JSON = "application/json";
#RequestMapping(value = "/getPersonDetails", method = RequestMethod.GET, produces = APP_JSON)
public String getPersonDetails() {
return "test";
}
}
web.xml
<?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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring3Sample</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>1</session-timeout>
</session-config>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
</web-app>
Can anyone please tell me some solution for this
UPDATE 1
web.xml
<?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_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Spring3Sample</display-name>
:
:
:
You have mapped the DispatcherServlet with URL pattern as
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Whenever you want t request a controller method you must have to add this URL pattern to the request. So that Spring will find the correct mapping this.
So change your request URL to following:-
http://localhost:8080/Spring3Sample/user/getPersonDetails.do
As you expect to return a test string:
Add the
#ResponseBody
annotation to ur controller's method. So that Spring wouldn't look for the JSP page with name test, instead return test as String.
EDIT:
From the discussion in chat:
Also u are missing <mvc:annotation-driven /> in applicationContext.xml.
To remove *.do from the request URL:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
After observing your applicationContext.xml , I can see you are missing <mvc:annotation-driven/> Your Updated code will look like below
<?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-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.controllers" />
<mvc:annotation-driven/>
</beans>
Explanation
<annotation-driven /> means that you can define spring beans dependencies without actually having to specify a bunch of elements in xml or implement an interface or extend a base class.Means #Controller tells spring that the the class specified contains methods that will handle http requests without you having to implement the Controller interface or extend a subclass that implements controller.
<context:component-scan base-package="com.controllers" /> tells spring that it should search the class path for all the classes under com.controllers and look at each class to see if it has a #Controller, or #Repository, or #Service, or #Component and if it does then Spring will register the class with the bean factory as if you have defined in in the xml configuration files
As per user comment
do we need both applicationContext.xml and dispatcher-servlet.xml
Actually Spring lets you define multiple contexts in a parent-child hierarchy.
applicationContext.xml defines the beans for the root webapp context means the context associated with the webapp.
dispatcher-servlet.xml defines the beans for one servlet's application context. There can be many dispatcher-servlet.xml in a webapp i.e servlet1-servlet.xml for servlet spring1, spring2-servlet.xml for servlet spring2
note:: Beans in spring-servlet.xml can reference beans in
applicationContext.xml, but not vice versa.
You missed port number in your URL
Use this URL
http://localhost:8080/Spring3Sample/user/getPersonDetails
First in Spring MVC manual
The #Controller annotation acts as a stereotype for the annotated
class, indicating its role. The dispatcher scans such annotated
classes for mapped methods and detects #RequestMapping annotations
(see the next section).
So delete this
#RequestMapping("/user")
Because it makes no sense next
#RequestMapping("/user/getPersonDetails")
you should note I add "/user" to url.
Also you are not return any jSon. First if I were you I change second request mapping (on the method getUserDetails) with string I put in this answer.
And looks like you miss with config files...
Check this string in web.xml
<url-pattern>*.do</url-pattern>
it will dispatch only urls with ".do" at their end.
I added this to my config class that extends WebMvcConfigurer:
#Bean
public ViewResolver viewResolver() {
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/view/pages/");
bean.setSuffix(".jsp");
return bean;
}
You'll have to add this dependency for it to work:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

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

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.

Spring MVC RequestMapping

I'm in the early stages of learning to use Spring MVC. I've created a controller and applied a RequestMapping annotation to it. When I run the project, the index page displays as expected at index.htm, but when I navigate to the URI that should be pointing to my controller, I get a 404 error, even though the controller seems to have been detected by Spring and started. Please help me understand what I am failing to grasp here:
Here is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<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>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
Here is my applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd>
<context:spring-configured/>
<context:component-scan base-package="org.blah.blah"/>
Here is my 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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
And, finally, here is my controller.
#Controller
#RequestMapping(value = "/hello")
public class Ctrl {
#RequestMapping(method = RequestMethod.GET)
#ResponseBody
public String hello(){
return "hello!";
}
}
EDIT: This is what my browser displays:
I was half-wrong in the comments to gravityplanx' answer.
When you specify a <mvc:annotation-driven /> in your servlet configuration, Spring registers a RequestMappingHandlerMapping bean which is meant to collect and map all your #Controller handler methods, ie. those annotated with #RequestMapping.
It does this by looking at all the beans in the current context. Remember that when you load a context with DispatcherServlet, call it the servlet context, if a context was loaded by a ContextLoaderListener, call it the root context, the root context is made a parent of the servlet context. By default, the RequestMappingHandlerMapping bean does not look at the root context, which is a parent of the servlet context.
In your case, the #Controller beans are declared implicitly in the root context (your applicationContext.xml) because of the <context:component-scan/> which scans classes annotated with
#Component or any of its specializations (#Controller for instance) and creates beans for them. So these beans are registered in the root context, where the RequestMappingHandlerMapping can't find them.
If instead you declare the <context:component-scan /> in the servlet context, then the #Controller (and other) beans are created in the servlet context where they are available to the RequestMappingHandlerMapping which can then register them to handle requests.
Note that <context:component-scan /> on its own doesn't do anything for the MVC stack. It needs further configuration, like <mvc:annotation-driven />. You should aim to specify packages that contain servlet specific beans in the servlet context and application wide beans in the root context.
Here's some literature:
What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?
Difference between applicationContext.xml and spring-servlet.xml in Spring Framework
Your dispatcher servlet doesn't seem to be referenced correctly.
Replace:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
With:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>[insert the path to your dispatcher-servlet here]</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
And then move:
<context:spring-configured/>
<context:component-scan base-package="org.blah.blah"/>
From applicationContext to dispatcher-servlet

Categories