1

I'm struggling with the issue described here

No mapping found for HTTP request with URI Spring MVC

Problem is once the resource resolver resolves the view, it's again redirected to the Dispatcher, which doesn't find the a method mapped to it.

For example if user requests for /test and resolver maps it to /views/test.jsp, dispatcher instead of rendering the response tries to lookup path /views/test.jsp again.

I have tried to change the default dispatcher servlet path to / (which by the way is / by default).

It doesn't work with Spring Boot configuration.

I would like to know if there's a solution for this without having to set dispatcher path to something other than / like /page.

Following is test code to produce the issue. (Here's github link for the Test project https://github.com/ConsciousObserver/stackoverflow/tree/master/TestMvc)

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
public class TestMvcApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestMvcApplication.class, args);
    }
}

@Configuration
class MvcConfig extends WebMvcConfigurerAdapter {
    public MvcConfig () {
        System.out.println("%%%%%%%%%%%%% " + getClass() + " loaded %%%%%%%%%%%%%%%");
    }

    @Bean
    public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
        ServletRegistrationBean registration = new ServletRegistrationBean(
                dispatcherServlet);
        System.out.println("%%%%%%%%%%%%%%%%% Adding dispatcher servletmapping");
        registration.addUrlMappings("/");
        return registration;
    }
}

@Controller
class TestController {
    @RequestMapping("test")
    public String test() {
        return "test";
    }
}

application.properties

logging.level.org.springframework.web=DEBUG

spring.mvc.view.prefix: /views/
spring.mvc.view.suffix: .jsp

Following are the relevant logs

Returning [org.springframework.web.servlet.view.InternalResourceView: name 'test'; URL [/views/test.jsp]] based on requested media type 'text/html'
Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'test'; URL [/views/test.jsp]] in DispatcherServlet with name 'dispatcherServlet'
Forwarding to resource [/views/test.jsp] in InternalResourceView 'test'
DispatcherServlet with name 'dispatcherServlet' processing GET request for [/views/test.jsp]
Looking up handler method for path /views/test.jsp
Did not find handler method for [/views/test.jsp]
Matching patterns for request [/views/test.jsp] are [/**]
URI Template variables for request [/views/test.jsp] are {}
Mapping [/views/test.jsp] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@21362712]]] and 1 interceptor
Last-Modified value for [/views/test.jsp] is: -1
Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
Successfully completed request
Successfully completed request
Community
  • 1
  • 1
11thdimension
  • 10,333
  • 4
  • 33
  • 71
  • Remove your `MvcConfig ` as that breaks proper integration, also do you really want to use JSPs? That would only work with `war` packaging and the jsp files would need to go into `webapp/views` in your case. – M. Deinum Jun 15 '16 at 06:05
  • Can you please elaborate how it's breaking the configuration, removing it doesn't help ? Yes I need JSPs as I'm converting a legacy project to the Spring Boot, it has a lot of JSPs, which I'm not yet ready to migrate to any other view layer. – 11thdimension Jun 15 '16 at 13:46
  • It's breaking because it disables (part of) the auto configuration which you probably still expect to work. – M. Deinum Jun 15 '16 at 13:50
  • What is the correct way to override MVC configuration in Spring Boot ? – 11thdimension Jun 15 '16 at 15:24
  • What would you need to override that isn't already configured? – M. Deinum Jun 16 '16 at 05:53
  • I can be needed for any custom configurations like registration of another servlet, or registration of abbriviated view mappings. – 11thdimension Jun 17 '16 at 14:14
  • Then you can extend the default you don't need to override it. – M. Deinum Jun 17 '16 at 16:24

2 Answers2

1

I finally found the solution.

Including following dependencies resolves the issue.

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

I'm guessing that there was a JSP parsing error internally (Or JSTLView wasn't present) which redirect the view request to the dispatcher.

After including these dependencies JSP view is rendered successfully.

11thdimension
  • 10,333
  • 4
  • 33
  • 71
0

To anyone who tried other solutions with no luck, you may want to check if ResponseBody annotation is present in the controller class.

@Controller
@EnableWebMvc
public class MainController {

    @Autowired
    BO bo = new BO();

    @RequestMapping(value = "/sayHello",
                    method = RequestMethod.GET, 
                    produces = "application/json")
    public @ResponseBody String sayHello() {
        return "Hellaa!!";
    }

}