在名称为'dispatcherServlet'的DispatcherServlet中找不到带有URI [/WEB-INF/JSP/restapis.jsp]的HTTP请求的映射

时间:2019-03-15 09:43:12

标签: spring-boot spring-mvc

我正在开发一个承载Rest API控制器和Web Mvc控制器的Spring Boot应用程序。

但是,当我尝试打开与Web Mvc控制器关联的网页localhost:8080 / discovery-service / v1 / health时,出现以下错误:

在Dispatcher中未找到具有URI [/WEB-INF/JSP/restapis.jsp]的HTTP请求的映射在DispatcherServlet中未找到具有URI [/WEB-INF/JSP/restapis.jsp]的HTTP请求的映射,名称为'dispatcherServlet '名称为'dispatcherServlet'的Servlet

这是WebConfig文件:

package com.skyscanner.discovery.config.web;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.BeanNameViewResolver;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableWebMvc
@ComponentScan("com.skyscanner*")
public class WebConfig {

    @Bean
    public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
        ContentNegotiatingViewResolver contentNegotiatingViewResolver
                = new ContentNegotiatingViewResolver();

        BeanNameViewResolver beanNameViewResolver = new BeanNameViewResolver();
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();

        internalResourceViewResolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
        internalResourceViewResolver.setPrefix("/WEB-INF/JSP/");
        internalResourceViewResolver.setSuffix(".jsp");

        List<ViewResolver> list = new ArrayList<ViewResolver>();
        list.add(beanNameViewResolver);
        list.add(internalResourceViewResolver);

        contentNegotiatingViewResolver.setViewResolvers(list);

        return contentNegotiatingViewResolver;
    }

    @Bean
    public ResourceBundleMessageSource messageSource(){
        ResourceBundleMessageSource messageSource=new ResourceBundleMessageSource();

        messageSource.setBasename("messages");
        return messageSource;
    }

    @Bean
    public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(5000000);
        return multipartResolver;
    }

    @Bean
    public LocalValidatorFactoryBean validator(){
        LocalValidatorFactoryBean validator=new LocalValidatorFactoryBean();
        validator.setValidationMessageSource(messageSource());
        return validator;
    }


    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver=new InternalResourceViewResolver();

        viewResolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
        viewResolver.setPrefix("/WEB-INF/JSP/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }

}

这是DiscoveryServiceBeanConfiguration.java文件:

package com.skyscanner.discovery.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@Configuration
public class DiscoveryServiceBeanConfiguration {

    @Bean
    public TaskScheduler taskScheduler(){
        ThreadPoolTaskScheduler threadPoolTaskScheduler=new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setPoolSize(5);
        threadPoolTaskScheduler.setThreadNamePrefix("ThreadPoolTaskScheduler");
        return threadPoolTaskScheduler;
    }
}

这是DiscoveryServiceWebInitializer.java文件:

package com.skyscanner.discovery.config;

import com.skyscanner.discovery.config.web.WebConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

@Order(1)
public class DiscoveryServiceWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

    Logger logger= LoggerFactory.getLogger(DiscoveryServiceWebInitializer.class);

    protected Class<?>[] getRootConfigClasses(){
        return new Class<?>[] {WebSecurityConfig.class,MongoConfig.class,SimpleMongoConfig.class};
    }

    protected Class<?>[] getServletConfigClasses(){
        return new Class<?>[] {WebConfig.class};
    }

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

这是ServletInitializer文件:

package com.skyscanner.discovery;


import com.skyscanner.discovery.config.DiscoveryServiceBeanConfiguration;
import com.skyscanner.discovery.config.web.WebConfig;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;

public class ServletInitializer implements WebApplicationInitializer{
    @Override
    public void onStartup(ServletContext container){
        AnnotationConfigWebApplicationContext rootContext=new AnnotationConfigWebApplicationContext();
        rootContext.register(DiscoveryServiceBeanConfiguration.class);

        container.addListener(new ContextLoaderListener(rootContext));
        AnnotationConfigWebApplicationContext dispatcherServlet=new AnnotationConfigWebApplicationContext();
        dispatcherServlet.register(WebConfig.class);

        ServletRegistration.Dynamic dispatcher=container.addServlet("dispatcher",new DispatcherServlet(dispatcherServlet));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

    }
}

这是HealthController.java文件:

package com.skyscanner.discovery.web.controller;

import com.skyscanner.discovery.domain.RestAPI;
import com.skyscanner.discovery.repository.RestAPIRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/discovery-service/v1")
public class HealthController {

    @Autowired
    private RestAPIRepository restAPIRepository;

    @RequestMapping("/health")
    public String monitorHealth(Model model){
        List<RestAPI> restAPIs=restAPIRepository.findAll();

        model.addAttribute("restAPIs",restAPIs);

        return "restapis";
    }
}

有src / main / resources / WEB-INF / JSP / restapis.jsp,src / main / WEB-INF / JSP / restapis.jsp和src / main / webapp / WEB-INF / JSP / restapis.jsp在系统中。

0 个答案:

没有答案