No mapping found for HTTP request with URI [/FitnessTracker/] - spring

I am getting an warnning in springmvc
as No mapping found for HTTP request with URI [/FitnessTracker/] in DispatcherServlet with name 'fitTrackerServlet'
INFO: FrameworkServlet 'fitTrackerServlet': initialization completed in 2194 ms
Feb 26, 2017 9:43:08 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/FitnessTracker/] in DispatcherServlet with name 'fitTrackerServlet'
When i press url http://localhost:8080/fitnessTracker/ I am getting 404 error.
Thanks
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<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>/*</url-pattern>
</servlet-mapping>
<display-name>Archetype Created Web Application</display-name>
</web-app>
and my 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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.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-3.2.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.pluralsight.controller"></context:component-scan>
<!--
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
</beans>
and my controller
#Controller
public class HelloController {
#RequestMapping(value = "/greeting")
public String sayHello (Model model) {
System.out.println("Test");
model.addAttribute("greeting", "Hello WorldX");
return "hello";
}
}

Does your component-scan base-package match the package where your HelloController is in? If not, that is the problem.
<context:component-scan base-package="com.pluralsight.controller"></context:component-scan>
Configures component scanning directives for use with #Configuration
classes. Either basePackageClasses() or
basePackages() (or its alias value()) may be specified to define
specific packages to scan.
Spring does not find your HelloController and so doesn't map anything.
Otherwise you could also follow these steps.

I am wondering why you have you application configured with XML. With this tool you can start a maven project with a Spring boot application that will be, by default, already configured to build exactly what you want.
Then you only need a class with:
#RestController
#RequestMapping("/fitnessTracker")
public class HelloController {
#RequestMapping(value = "/greeting")
public String sayHello (Model model) {
System.out.println("Test");
model.addAttribute("greeting", "Hello WorldX");
return "hello";
}
}
And that's it, Spring will do the rest for you ;)

In my case the problem was that i was using this url-pattern
<url-pattern>/*</url-pattern>
instead of this
<url-pattern>/</url-pattern>
and also i had to add
#RequestMapping(value = "/fitnessTracker")
under the
#Controller
annotation.
I found the solution at : https://www.baeldung.com/spring-mvc-404-error for the pattern.

Related

Spring MVC #GetMapping("") is executed three times in the first request

I've been working with Spring, I don't understand what the problem is. In the Test.class Spring MVC controller the code (the default controller, the first request will receive it) in the get request the code is executed three times this method includes 3 threads or 3 get requests.
Synchronized method and SingleThreadModel interface don't help, so I think the problem is with three requests?
If I execute the code in a different controller (not in the default fetch request) everything works good.
Test.class
#Controller
public class Test {
#GetMapping("")
public String hello(ModelMap model) {
System.out.println("method hello said 'yes'"); //runs twice
return "index";
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0" 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_4_0.xsd">
<display-name>Spring MVC</display-name>
<servlet>
<servlet-name>PredictWeather</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:servletsConfig.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PredictWeather</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
servletsConfig.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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="servlets"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
logs
Perhaps someone has come across, or knows how to solve the problem, thanks for any answer! I ask the question for the first time, perhaps it is poorly formulated

Spring mvc Component scan requestmapping not working

I am new to Spring and Spring-MVC. I am using 4.3.3.RELEASE.I have generated project using maven-webapp-archetype and added sping dependencies and dispatcher-servelt later.
As per my web.xml I have initialized Dispatcher-servlet using spring-dispatcher-servlet.xml
I have already tried
Added inet.controller.* instead inet.controller in base-package
Changing URL pattern to /* from /
Adding to dispatcher servlet
but still when I try http://loclahost:8080/inet/welcome server is throwing 404.
Web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Spring-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<mvc:default-servlet-handler />
<context:component-scan base-package="inet.controller"/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Below is my directory structure:
HelloController
package inet.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloController {
#RequestMapping("/welcome")
public ModelAndView helloWorld() {
ModelAndView modelAndView = new ModelAndView("helloPage");
modelAndView.addObject("message", "Hi All");
return modelAndView;
}
}
You need to tell the spring how to find the controllers from urls that you are hitting. So it what is missing in your configuration
to do that add <mvc:annotation-driven/> tag in your dispatcher xml configuration. it adds some mapping handlers
RequestMappingHandlerMapping
RequestMappingHandlerAdapter
ExceptionHandlerExceptionResolver
And some necessary message converters for you.
What does <mvc:annotation-driven /> do?

No mapping found for HTTP request with URI [/TEST2/] in DispatcherServlet with name 'dispatcher' [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 am creating a simple login page with model and controller using maven, Spring 3.1.1. I have just created a model and controller. But while running the application I get a 404 error in my browser and in console I am getting the following error:
org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TEST2/] in DispatcherServlet with name 'dispatcher'
I have checked the configuration properly and I couldn't find the exact error for this.
I have changed some of the configuration. I tried putting /* in URL-Mapping but I am facing the same issue.
My Web.XML
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app 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"
version="2.4">
<display-name>Spring MVC Application</display-name>
<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>
<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>
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" 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">
<context:component-scan base-package="com.concretepage.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
LoginController.Java
package com.concretepage.controller;
import javax.servlet.http.HttpServletRequest;
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 LoginController {
#RequestMapping(value="/login", method = RequestMethod.GET)
public String login(){
return "redirect:pages/login.jsp";
}
#RequestMapping(value="pages/userCheck", method = RequestMethod.POST)
public String userCheck(ModelMap model, HttpServletRequest request) {
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
if("concretepage".equalsIgnoreCase(name)&&"concretepage".equalsIgnoreCase(pwd)){
model.addAttribute("message", "Successfully logged in.");
}else{
model.addAttribute("message", "Username or password is wrong.");
}
return "redirect:success.jsp";
}
}
What's my mistake?
You have not placed the Mapping for root path '/'
change this code
#RequestMapping(value="/login", method = RequestMethod.GET)
public String login(){
return "redirect:pages/login.jsp";
}
into
#RequestMapping(value="/", method = RequestMethod.GET)
public String login(){
return "redirect:pages/login.jsp";
}
and check the output.

Spring 4.2.1 RestController tried to return template instead return JSON

I tried to create a Spring Rest Controller, based on this example i create a controller like this.
DeveloperRestController.java
#RestController
public class DeveloperRestController {
#RequestMapping("/developer/list")
public Developer index() {
Developer developer = new Developer("Developername", "developer#yahoo.com");
return developer;
}
}
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">
<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>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!--Here we specify about the DispatcherServlet class in the Web Deployment Descriptor-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
</web-app>
dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="com.developerdata.controller" />
<context:annotation-config />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
But it shows 404 page not found, seems that spring tried to load a template...
Result:
Error 404 /WEB-INF/jsp/developer/list.jsp
what should i do?
Seems like you have a configuration issue. The sample you are basing yours on is spring boot based. So it handles the configuration for you. To get yours working you will need to add jackson to the classpath. If you are using maven then:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.2</version>
</dependency>
Then you need to alter your spring config to include:
<mvc:annotation-driven />
From the spring documentation:
The above registers a RequestMappingHandlerMapping, a
RequestMappingHandlerAdapter, and an ExceptionHandlerExceptionResolver
(among others) in support of processing requests with annotated
controller methods using annotations such as #RequestMapping,
#ExceptionHandler, and others.
This also then enables the MappingJackson2HttpMessageConverter if jackson 2 is in your classpath.
References:
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-config-enable
https://spring.io/guides/gs/rest-service/
http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/ (This one seems more appropriate for you to get started based on your problem)
With #ResponseBody, Spring will handle the JSON you need. Jackson
library is required too.
#RestController
public class DeveloperRestController {
#RequestMapping("/developer/list")
public #ResponseBody Developer index() {
Developer developer = new Developer("Developername", "developer#yahoo.com");
return developer;
}
}

No mapping found for HTTP request with URI [/HelloWeb/WEB-INF/jsp/hello.jsp]

I tried to to load a simple example of spring mvc with a simple jsp but failed with the following error: No mapping found for HTTP request with URI [/HelloWeb/WEB-INF/jsp/hello.jsp]
I'm using Spring 3.2.2.
I really appreciate your help here.
I tried several thing but still it seems that I can not map my jsp page in the controller.
web.xml looks like that:
<web-app id="WebApp_ID" 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">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
</web-app>
HelloWeb-servlet.xml looks like that:
<beans xmlns="http://www.springframework.org/schema/beans"
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-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.tutorial.spring" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
HelloControll.class looks like that:
package com.tutorial.spring;
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
#RequestMapping("/hello")
public class HelloController {
#RequestMapping(method= RequestMethod.GET)
public String printHello(ModelMap modelMap) {
modelMap.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
Correct URL to hit your printHello handler method is
/HelloWeb/hello.jsp
I figured it out. foolish mistake.
I named my jsp "Hello.jsp" with capital 'H' and returned in my controller 'hello' with lower case.
Just changed my jsp page to be "hello.jsp".
thanks for your help.
Modify web.xml like this
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Try to access /YourAppName/hello. Then Spring-MVC will trigger the method printHello() . Then return the jsp view in /WEB-INF/jsp/.
The dispatcher servlet have to map the correct main route:
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
move jsp file to right place as defined in servlet
/WEB-INF/jsp/
The correct URL is http://localhost:8080/hello, without "/HelloWeb"

Resources