Request is not mapped to the controller - spring

When I go with the url..http://localhost:8080/springdemo/hello..it is showing 404 not found error..I have put my java file inside src/main/java as usual in Maven project. My controller code is as follows :-
package org.abhishek;
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;
import java.lang.System;
#Controller
public class HelloWorldController {
#RequestMapping(value="/hello", method = RequestMethod.GET)
public ModelAndView helloWorld() {
System.out.println("hello**");
String message = "Hello World, Spring MVC # Javatpoint";
return new ModelAndView("hello", "message", message);
}
}
Web.xml file given below
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="java.sun.com/xml/ns/javaee"; xmlns:xsi="w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="java.sun.com/xml/ns/javaee 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>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
I put this line System.out.println() for debugging purpose and I found that this method is not executed with the above mentioned url...i.e. http://localhost:8080/springdemo/hello.Please answer..thanx in advance.

This is speculative, but you could have a mapping problem in your web.xml file, which would result in the Spring controller not even being hit (despite having a correct #RequestMapping annotation). Your web.xml file should have the following servlet mapping:
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/springdemo/*</url-pattern>
</servlet-mapping>
Now paste your URL into a web browser and see if you can hit it:
http://localhost:8080/springdemo/hello

Related

Auto Repository with Spring Data

I have a little problem in integrating spring with JSF.
I understood that either Spring and JSF have distinct containers. So I can't mix their annotations because registered beans will not be visible to each other.
However, I've read an article : Spring DAO is not injected in JSF managed bean where guy says that It's possible to annotate everything with spring annotations make visible these components to JSF.
I'm really confused with xml configuration because sometimes I read that I don't need it but in most cases I see people usually do this.
My question is what need to be configured if I have this source code and I want to make visible spring beans to JSF :
package pl.catarina.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import pl.catarina.domain.Patient;
import pl.catarina.repository.PatientRepository;
import javax.annotation.PostConstruct;
#Component
#Scope("request")
public class RegisterController {
#Autowired
private PatientRepository patientRepository;
private Patient patient;
#PostConstruct
public void init(){
patient = new Patient();
patient.setName("jan");
patient.setSurname("Way");
patient.setPhoneNumber("21421131");
patient.setEmail("asdpad#gmail.com");
patient.setPassword("pdsad223Sdsd");
}
public void save(){
patientRepository.save(patient);
}
}
REPOSITORY
package pl.catarina.repository;
import org.springframework.stereotype.Repository;
import pl.catarina.domain.Patient;
#Repository
public interface PatientRepository extends UserBaseRepository<Patient> {
Patient findByEmail(String email);
}
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">
<welcome-file-list>
<welcome-file>pages/home.xhtml</welcome-file>
</welcome-file-list>
<!--
FacesServlet is main servlet responsible to handle all request.
It acts as central controller.
This servlet initializes the JSF components before the JSP is displayed.
-->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<context-param>
<param-name>com.ocpsoft.pretty.DEVELOPMENT</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>bootstrap</param-value>
</context-param>
</web-app>

REST Web Service not working

I am just trying to create a simple test web service. I want to access the User in XML format at the following url: http://localhost:8080/Online_Shopping/dispatcher/rest/hello, but when I go to the URL nothing is displayed.
Here is my code for the service:
package com.shopping.controller;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.shopping.model.User;
#Path("/rest")
public class RESTController {
#GET
#Path("/hello")
#Produces(MediaType.APPLICATION_XML)
public User getUser() {
return new User("paymon","123",true);
}
}
The following code is in my spring-config.xml
<context:component-scan base-package="com.shopping.controller" />
This is in my web.xml
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/dispatcher/*</url-pattern>
</servlet-mapping>
Your application is running on port 8080 , so to access this URL you need to add in your servlet dispatcher the "Online_Shopping"
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>Online_Shopping/dispatcher/*</url-pattern>
</servlet-mapping>
or Just access your api without Online_Shoppping
You have to use Spring MVC's annotations instead and use #Controller or #RestController to annotate you controller to be registered as a spring controller.
Try this :
#RestController
#RequestMapping(value = "/rest")
public class RESTController {
#RequestMapping(value = "/hello",
method = RequestMethod.GET,
produces = "application/xml")
public User getUser() {
return new User("paymon","123",true);
}
}

Webservlet suddenly stopped working

I have got a Webservlet (Dynamic Web Module 3.1), it looks like this:
package de.timetoact.cce;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ibm.sbt.services.client.ClientServicesException;
import de.timetoact.cce.handler.ContentHandler;
import de.timetoact.cce.util.Connect;
import de.timetoact.cce.util.Util;
import de.timetoact.cce.util.Variables;
#WebServlet(urlPatterns = { "/main" }, loadOnStartup = 1)
public class InitServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String actionn = request.getParameter("action").toLowerCase();
if (actionn.equals("delete")) {
System.out.println("It does work");
}
}
}
Web.xml:
<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">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<description>The service servlet handles requests from the toolkit to access external resources.</description>
<display-name>Social Business Toolkit Service Servlet</display-name>
<servlet-name>ServiceServlet</servlet-name>
<servlet-class>com.ibm.sbt.service.core.servlet.ServiceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServiceServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<servlet>
<description>This servlet initializes the specified JavaScript library for use by the Social Business Toolkit.</description>
<display-name>Social Business Toolkit Library Servlet</display-name>
<servlet-name>LibraryServlet</servlet-name>
<servlet-class>com.ibm.sbt.jslibrary.servlet.LibraryServlet</servlet-class>
<init-param>
<param-name>toolkitExtUrl</param-name>
<param-value>%local_server%/sbtx</param-value>
</init-param>
<init-param>
<param-name>jsLibraryUrl</param-name>
<param-value>%local_server%/sbt/js/libs</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>LibraryServlet</servlet-name>
<url-pattern>/library/*</url-pattern>
</servlet-mapping>
<filter>
<description>This filter is responsible for creating the toolkit application and context objects for every servlet within this web application.</description>
<display-name>Social Business Toolkit Filter</display-name>
<filter-name>SBTFilter</filter-name>
<filter-class>com.ibm.sbt.util.SBTFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SBTFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Yesterday it worked perfectly, but today I get the message:
The requested resource is not available. /CCE/main
I cleaned the project and did update (Maven Update Project)
Nevertheless I got this context error, although I did not change anything.
What is wrong here?
This problem is caused by running two instances of your project in eclipse or tomcat. You can resolve this problem by restarting eclipse or tomcat.

how to handle url that are not mapped in spring

My dispatcher servlet mapping
<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/springconfig/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
And the controller has handler like
#RequestMapping("moduleone")
public class ApplicationController {
#RequestMapping(value="Login.html",method=RequestMethod.GET)
public ModelAndView showLoginPage(){
ModelAndView mv=new ModelAndView("../moduleone/Login");
mv.addObject("loginForm", new LoginForm());
return mv;
}
#RequestMapping(value="Home.html", method = RequestMethod.GET)
public ModelAndView showHome(HttpServletRequest request) {
ModelAndView mv=new ModelAndView("Home");
mv.addObject("customerName",appCon.getFirstName() );
return mv;
}
}
Is it possible to handler request that are not mapped in controller
like
http://localhost:8090/Project/moduleone/invalidpage.html
http://localhost:8090/Project/moduleone/invalidurl/invalidpage
I have tried #RequestMapping(value="*",method=RequestMethod.GET) but doest work
As 404 (page not found) actually produces an exception on web container level, containers usually provide an exception handling mechanism, thus you can try exception (or so called error) handling, as shown below;
First create a controller
#Controller
public class PageNotFoundErrorController {
#RequestMapping(value="/pageNotFound.html")
public String handlePageNotFound() {
// do something
return "pageNotFound";
}
}
and configure web.xml in order to map the error to the controller written above;
<error-page>
<error-code>404</error-code>
<location>/pageNotFound.html</location>
</error-page>
you can also extend it by simply adding 403, 500 and other error-codes to web.xml and mapping them to any controller.
What is even more fascinating is that you can also map any exception (even the ones created by your code); here you can find a nice example about it http://www.mkyong.com/spring-mvc/spring-mvc-exception-handling-example/
I try the code block and if change your scenario a bit i can handle it.
//This one is OK
http://localhost:8090/Project/moduleone/invalidpage.html
//add invalid.html not a folder it should be file
http://localhost:8090/Project/moduleone/invalidurl/invalidpage.html
HomeController.java
#RequestMapping(value = {"*/*.html","*.html"}, method = RequestMethod.GET)
public String test(HttpServletResponse response) throws IOException {
return new String("home");
}
dispatcher-servlet.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">
<display-name>TestSpringMVC</display-name>
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>SpringDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springconfig/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringDispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
I can handle both request with this way.
I think you should define an exception page for your second scenario.
Also you can read this issue

Can not connect JAX-RS service to MVC template

I'm attempting to make use of JAX-RS' (Jersey) MVC pattern. Attempts to reach http://localhost:8080/myproject/foos/test result in an error that reads:
java.io.IOException: The template name, /view, could not be resolved to a fully qualified template name
http://localhost:8080/myproject/foos results in the same error.
What am I missing?
Resource:
package resources;
import com.sun.jersey.api.view.Viewable;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("foos")
public class FooResource {
#GET
#Produces(MediaType.TEXT_HTML)
public Viewable get() {
return new Viewable("/index", this);
}
#GET
#Path("{id}")
#Produces(MediaType.TEXT_HTML)
public Viewable get(#PathParam("id") String id) {
return new Viewable("/view", id);
}
}
Views:
WEB-INF / jsp / resources / FooResource
index.jsp
view.jsp
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">
<filter>
<filter-name>jersey</filter-name>
<filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
<init-param>
<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
<param-value>/(resources|images|js|styles|(WEB-INF/jsp))/.*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jersey</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<description>Set the default, base template path to the WEB-INF folder.</description>
<param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
<param-value>/WEB-INF/jsp</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Made the following changes:
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">
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>jersey</filter-name>
<filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>controllers</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
<param-value>/((WEB-INF/views))/.*</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
<param-value>/WEB-INF/views/</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.Redirect</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.FilterForwardOn404</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jersey</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Resource:
#Path("foos")
public class FooResource {
#GET
#Produces(MediaType.TEXT_HTML)
public Viewable index() {
return new Viewable("/foos/index", this);
}
#GET
#Path("{id}")
#Produces(MediaType.TEXT_HTML)
public Viewable view(#PathParam("id") String id) {
return new Viewable("/foos/view", id);
}
}
Views:
\welcome.jsp
\WEB-INF\views\foos\
index.jsp
view.jsp
I had this same error running under Jetty 9. The application ran fine using mvn clean jetty:run but had this error when packaged as a war and deployed under Jetty. This is the fix in web.xml that worked for me:
<init-param>
<param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
- <param-value>/WEB-INF/views/</param-value>
+ <param-value>/WEB-INF/views</param-value>
</init-param>
Yep, that's it. So, hopefully this helps someone who stumbles across this. My config is basically the same as craig's, but had the extra slash.
From initial inspection I think you want to put index.jsp and view.jsp directly in WEB-INF/jsp.
The name should be a fully qualified name like /index.jsp or /index.html.

Resources