Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring mvc - How to map all wrong request mapping to a single method

I am using a spring(version 3.2.5) annotation driven mvc application, basically I need be able to catch any wrong url to a single method then display an error message. For instance, if my correct url www.url.valid/login maps to a controller, if the user mistakenly insert loginw or any invalid url it should map to a default controller.

I have tried the following way:

package com.controller;

import static com.mns.utils.Constants.REGISTER_FAIL;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DefaultSpringController {

      @RequestMapping
      public String forwardRequest(final HttpServletRequest request) {
         return "redirect:/" + REGISTER_FAIL;
      }

}

My Application context is as follows:

    <!-- Default to DefaultSpringController -->
     <bean id="defaultSpringController"  class="com.controller.DefaultSpringController">
     </bean>

  <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="defaultHandler" ref="defaultSpringController"/>
  </bean

But kept getting the following error:

nested exception is java.lang.ClassNotFoundException: com.controller.DefaultSpringController

I am not sure what I am missing here, and the controller is in the correct package.

Any idea how I can achieve it please?

Thanks in advance

like image 791
user1999453 Avatar asked Nov 09 '16 10:11

user1999453


People also ask

Can we use @RequestMapping at method level?

The @RequestMapping annotation can be applied to class-level and/or method-level in a controller. The class-level annotation maps a specific request path or pattern onto a controller. You can then apply additional method-level annotations to make mappings more specific to handler methods.

What is the difference between GetMapping and RequestMapping?

@RequestMapping is used at the class level while @GetMapping is used to connect the methods. This is also an important Spring MVC interview question to knowing how and when to use both RequestMapping and GetMapping is crucial for Java developers.

What is the @RequestMapping annotation used for?

RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.

Which of the following is a method where RequestMapping is used with multiple URI?

@RequestMapping With Multiple URIs You can have multiple request mappings for a method. For that add one @RequestMapping annotation with a list of values. As you can see in this code, @RequestMapping supports wildcards and ant-style paths.


2 Answers

It look like, you want a global page not found handler.

@ControllerAdvice
public class GlobalExceptionContoller {

    @ExceptionHandler(NoHandlerFoundException.class)
    public String handleError404(HttpServletRequest request, Exception e) {
        return "pageNotFoundViewNameGoesHere";
    }

}
like image 180
Afsun Khammadli Avatar answered Oct 18 '22 10:10

Afsun Khammadli


You can set config at web.xml for invalid url. Note that invalid url usually throw the 404 error page.

<error-page>
        <error-code>404</error-code>
        <location>/error/error.jsp</location>
    </error-page>

If it's not enough, you can go your controller method from this error page. But I usually use this config for error page instead of server side code. There have many other alternatives ways depend on your technologies usage but this way can reduce your code.

like image 27
Ye Win Avatar answered Oct 18 '22 11:10

Ye Win