解析来自handler [null]的异常:org.springframework.web.servlet.NoHandlerFoundException:找不到GET的处理程序

时间:2017-05-03 09:24:20

标签: java spring spring-mvc

可以帮我解决这个问题。我正在使用spring 4 maven项目,spring java cofigaration。模型和视图不起作用。我从2天开始就遇到了这个问题,对此我已经筋疲力尽了。

2017-05-03 17:11:55 DEBUG DispatcherServlet:861 - DispatcherServlet with name 'dispatcher' processing GET request for [/iservefds/hello]
2017-05-03 17:11:55 DEBUG RequestMappingHandlerMapping:320 - Looking up handler method for path /hello
2017-05-03 17:11:55 DEBUG RequestMappingHandlerMapping:327 - Returning handler method [public org.springframework.web.servlet.ModelAndView com.iserve.fds.controller.HomeController.showMessage(java.lang.String)]
2017-05-03 17:11:55 DEBUG DefaultListableBeanFactory:250 - Returning cached instance of singleton bean 'homeController'
2017-05-03 17:11:55 DEBUG DispatcherServlet:947 - Last-Modified value for [/iservefds/hello] is: -1
in controller
2017-05-03 17:11:55 DEBUG DefaultListableBeanFactory:1620 - Invoking afterPropertiesSet() on bean with name 'home'
2017-05-03 17:11:55 DEBUG DefaultListableBeanFactory:250 - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2017-05-03 17:11:55 DEBUG DispatcherServlet:1241 - Rendering view [org.springframework.web.servlet.view.JstlView: name 'home'; URL [/WEB-INF/views/home.jsp]] in DispatcherServlet with name 'dispatcher'
2017-05-03 17:11:55 DEBUG JstlView:432 - Added model object 'message' of type [java.lang.String] to request in view with name 'home'
2017-05-03 17:11:55 DEBUG JstlView:432 - Added model object 'name' of type [java.lang.String] to request in view with name 'home'
2017-05-03 17:11:55 DEBUG JstlView:166 - Forwarding to resource [/WEB-INF/views/home.jsp] in InternalResourceView 'home'
2017-05-03 17:11:55 DEBUG DispatcherServlet:861 - DispatcherServlet with name 'dispatcher' processing GET request for [/iservefds/WEB-INF/views/home.jsp]
2017-05-03 17:11:55 DEBUG RequestMappingHandlerMapping:320 - Looking up handler method for path /WEB-INF/views/home.jsp
2017-05-03 17:11:55 DEBUG RequestMappingHandlerMapping:330 - Did not find handler method for [/WEB-INF/views/home.jsp]
2017-05-03 17:11:55 WARN  PageNotFound:1136 - No mapping found for HTTP request with URI [/iservefds/WEB-INF/views/home.jsp] in DispatcherServlet with name 'dispatcher'
2017-05-03 17:11:55 DEBUG ExceptionHandlerExceptionResolver:131 - Resolving exception from handler [null]: org.springframework.web.servlet.NoHandlerFoundException: No handler found for GET /iservefds/WEB-INF/views/home.jsp
2017-05-03 17:11:55 DEBUG ResponseStatusExceptionResolver:131 - Resolving exception from handler [null]: org.springframework.web.servlet.NoHandlerFoundException: No handler found for GET /iservefds/WEB-INF/views/home.jsp
2017-05-03 17:11:55 DEBUG DefaultHandlerExceptionResolver:131 - Resolving exception from handler [null]: org.springframework.web.servlet.NoHandlerFoundException: No handler found for GET /iservefds/WEB-INF/views/home.jsp
2017-05-03 17:11:55 WARN  DefaultHandlerExceptionResolver:186 - Handler execution resulted in exception: No handler found for GET /iservefds/WEB-INF/views/home.jsp
2017-05-03 17:11:55 DEBUG DispatcherServlet:1034 - Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
2017-05-03 17:11:55 DEBUG DispatcherServlet:1000 - Successfully completed request
2017-05-03 17:11:55 DEBUG DispatcherServlet:1000 - Successfully completed request

ApplicationConfig.java

@Configuration
@EnableWebMvc
@ComponentScan(useDefaultFilters=true, basePackages={ ConfigConstants.BASE_PACKAGE }
    , includeFilters=@ComponentScan.Filter(type=FilterType.REGEX, pattern={ ConfigConstants.BASE_PACKAGE_REPO + ".*",ConfigConstants.BASE_PACKAGE_SERVICE + ".*"}))
public class ApplicationConfig extends WebMvcConfigurerAdapter{

private static final Logger LOGGER = Logger.getLogger(ApplicationConfig.class);

@Override
@Description("HTTP Message Converters")
public void configureMessageConverters(final List<HttpMessageConverter<?>> converters) {
    super.configureMessageConverters(converters);       
    converters.add(new MappingJackson2HttpMessageConverter());
}

@Bean public Mapper dozerMapper() { return new DozerBeanMapper(); }
@Bean public ObjectMapper objectMapper() { return new ObjectMapper(); }

@Bean
public MultipartResolver multipartResolver() {
    org.springframework.web.multipart.commons.CommonsMultipartResolver multipartResolver = new org.springframework.web.multipart.commons.CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(1000000);
    return multipartResolver;
}

@Bean
public ViewResolver viewResolver(){
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}

}

ApplicationInitializer.java

public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class[] { 
            ApplicationConfig.class
        };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class[] {};
}

@Override
protected String[] getServletMappings() {
    return new String[] { "/*" };
}

@Override
protected Filter[] getServletFilters() {
    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setEncoding("UTF-8");

    return new Filter[] { 
            characterEncodingFilter
            , new HiddenHttpMethodFilter() 
            , new ShallowEtagHeaderFilter()
            //, new ErrorHandlerFilter()
            , new CompressingFilter()
            //, new MultiReadServletFilter()
    };
}

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    //servletContext.addListener(DbConnectionClosedHandler.class);
    super.onStartup(servletContext);
}

@Override
public void customizeRegistration(ServletRegistration.Dynamic registration) {
    registration.setInitParameter("throwExceptionIfNoHandlerFound", "true");
}

}

HomeController.java

@Controller
public class HomeController {

String message = "Welcome to Spring MVC!";

@RequestMapping("/hello")
public ModelAndView showMessage(@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
    System.out.println("in controller");

    ModelAndView mv = new ModelAndView("home");
    mv.addObject("message", message);
    mv.addObject("name", name);
    return mv;
  }
}

2 个答案:

答案 0 :(得分:1)

ApplicationConfig 中添加以下内容:

@Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

并更改此ViewResolver:

@Bean
public ViewResolver viewResolver(){
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}
由InternalResourceViewResolver:

@Bean
    public InternalResourceViewResolver getInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

答案 1 :(得分:0)

您的主要错误在于:

@Override
protected String[] getServletMappings() {
   return new String[] { "/*" };
}

应该是:

@Override
protected String[] getServletMappings() {
   return new String[] { "/" };
}