Я разрабатываю приложение Spring Boot, которое содержит контроллер API Rest и веб-контроллер Mvc.
Однако, когда я пытаюсь открыть веб-страницу localhost: 8080 / discovery-service / v1 / health, котораясвязанный с контроллером Web Mvc, я получаю следующую ошибку:
Не найдено сопоставление для HTTP-запроса с URI [/WEB-INF/JSP/restapis.jsp] в DispatcherNo сопоставление найдено для HTTP-запроса с URI [/ WEB-INF / JSP / restapis.jsp] в DispatcherServlet с именем dispatcherServlet'Servlet с именем dispatcherServlet '
Вот файл 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 в системе.