Controller does't work in Spring web error 404 - spring

I do this lesson but my controller don't work error 404, before it worked well. jsp files work well. does't work only controller. The console has no errors.
What happened?
Controller:
#Controller
public class HelloController {
#RequestMapping(value = "/hello", method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
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"
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>Servlets</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:
<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.tutorialspoint" />
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp/" />
<property name = "suffix" value = ".jsp" />
</bean>
</beans>
hello.jsp
<html>
<head>
<title>Hello Spring MVC</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
Folder structure

A couple of things I'm noticing.
1) An <?xml tag declaration within a web.xml file is actually not allowed. Your JSP editor within Eclipse should have indicated an error. Please remove <?xml version="1.0" encoding="UTF-8"?>
2) I think you are running into issues between default servlet container handlers and mappings for the Dispatcher servlet. Can you change the servlet mapping in your web.xml file from
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
to
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
which is the default handling for Spring dispatcher.
I would perform those changes, perform a rebuild of your project, redeploy to your container and try hitting your controller mapping http://<yourhost_root>/HelloWeb/hello/

Related

Postman returning 404 for REST API on correct path

I have created a Rest API. I was testing it on postman yesterday and it was working fine, today it keeps returning a 404, the file paths haven't changed so that can't be wrong. I am using tomcat, and my xampp is on.
It postman is returning the correct data for another application I have.
The path I am trying to test is
http://localhost:8080/Assignment2C/breweries
the method I'm trying to test is
#RestController
#RequestMapping("/breweries")
public class Breweries_Controller {
#Autowired
Breweries_Service service;
#GetMapping(produces = MediaTypes.HAL_JSON_VALUE)
public Resources<Breweries> getAllBreweries() {
List<Breweries> allBreweries = service.getAllBreweries();
for (Breweries b : allBreweries) {
int id = b.getResourceId();
Link self = linkTo(this.getClass()).slash(id).withSelfRel();
b.add(self);
linkTo(methodOn(this.getClass()).getBrewerie(id));
}
Link link = linkTo(this.getClass()).withSelfRel();
Resources<Breweries> result = new Resources<Breweries>(allBreweries, link);
return result;
}
I am using JPA to connect to the database.
the project structure is
the config file is
<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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="Assignment2C" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/views</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<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/sd4-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
try to give path to the get mapping also
#GetMapping("/getAll")
you url will should look like
this:
http://localhost:8080/Assignment2C/breweries/getAll
Or
http://localhost:8080/2C/breweries/getAll
if i set the base_package to
<context:component-scan base-package="main" />
it works

noHandlerFound and showing 404 with no message in spring mvc

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"
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>withDiffServletName</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
example-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-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.app.simple" />
<mvc:annotation-driven />
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/" />
<property name = "suffix" value = ".jsp" />
</bean>
</beans>
controller
#Component
public class ConrrollController
{
#RequestMapping(value = "/student", method = RequestMethod.GET)
public ModelAndView student() {
return new ModelAndView("student", "command", "hi");
}
}
index.jsp
/student
student.jsp
hi
hi all,
i am new in spring ,this is my 1 st program an getting an error
org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/webroot/student] in DispatcherServlet with name 'sample'.
please provide me solution.an and encourage me .
i didnt understood
Why does Spring MVC respond with a 404 and report "No mapping found for HTTP request with URI [...] in DispatcherServlet"?
No handler mapping is came because the controller not returning view .
So we need to change #controller not #components

Why request is not transfer'd from hello.jsp to controller?

I am new to MVC.
I am trying to build a simple hello world application using Spring MVC.
I am getting 404 error as href doesn't goes to hello.jsp. I have my hello.jsp under my WEB-INf and index,jsp is under Web apps. Below is my code for the application. Thank you all in advance
#Controller
public class HelloController {
#RequestMapping(value = "/hello",method = RequestMethod.POST)
public ModelAndView mymethod(){
return new ModelAndView("hello","msg","Hello First Spring");
}
}
/////////////////////////// spring.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"
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.javatpoint"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
///////////////////////// 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>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/.jsp</url-pattern>
</servlet-mapping>
</web-app>
//////////////////////////// index.jsp ////////////////////////////
<html>
<body>
<h2>Hello World!</h2>
<%= java.util.Calendar.getInstance().getTime() %>
click here
</body>
</html>
//////////////////////////// hello.jsp /////////////////////////
Message is: ${msg}]
[1]: https://i.stack.imgur.com/XUJ9j.png
Change your controller to GET
#RequestMapping(value = "/hello",method = RequestMethod.GET)
public ModelAndView mymethod(){
return new ModelAndView("hello","msg","Hello First Spring");
}
Also change your web.xml
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

WARNING : No mapping found for http request with uri [/Authentication/] in dispatcherservlet with name 'spring' [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'm trying to make a simple web application which has a login and a welcome page using Spring MVC. The code is as follows:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-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"
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.test"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
Controller.java
#Controller
#RequestMapping("/Authentication")
public class TestController{
#RequestMapping(value="/")
public String Login(){
return "Login";
}
#RequestMapping(value="Authenticate", method=RequestMethod.GET)
public String Authenticate(){
//Authenticates and returns "Welcome"
}
}
The project name is Authentication. There are Login.jsp and Welcome.jsp under /WEB-INF/jsp/.
However, when I'm trying to run the project, I am getting HTTP Status 404 error and the following warning:
org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING : No mapping found for http request with uri [/Authentication/] in dispatcherservlet with name 'spring'
Why am I getting this warning even though the mappings look fine?
You are not specifying where the spring-servlet will be located. Add these to your web.xml file:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-servlet.xml
</param-value>
</context-param>

url-pattern doesn't work in spring mvc application

Spring 3.2 in apache tomcat
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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/rest-servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
rest-servlet-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
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">
<annotation-driven />
<context:component-scan base-package="com.*" />
</beans:beans>
My Controller:
#Controller
#RequestMapping(value = "/rest/person")
public class PersonController {
#RequestMapping(value = "/test")
#ResponseBody
public String getTest() {
return "Test controller";
}
}
When i call /localhost:8080/rest/person/test, i get 404 error (page not found).
However, when i change the url-pattern in web.xml file to:
<url-pattern>/*</url-pattern>
it works fine.
How can i make the first url-pattern work??
Remove rest from your RequestMapping, e.g. change it to
#RequestMapping(value = "/person")
The reason for this is that your url-pattern already contains rest/*, so what you were doing was to map to the URL rest/rest/person/test and not rest/person/test.

Resources