[英]Spring 5 MVC not finding mapping to controller returning JSON
我是 Spring 5 的新手,我一直在研究有關 Spring 5 MVC 的所有不同站點,盡可能多地學習,但仍然無法得到“http://localhost/[webcontext]/secure”的響應/json/組織”。 我最終得到了這個:[osweb.servlet.DispatcherServlet] Completed 404 NOT_FOUND。 我可以判斷該類已加載並且自動裝配完成,因為我必須解決這些問題。 下面是我的代碼。 我錯過了什么或做錯了什么?
注冊RestController.java
package c.i.i.w.e.controllers;
@RestController
public class EnrollmentRestController extends AbstractIfactoryController
{
@GetMapping(path = "/secure/json/organizations", produces = MediaType.APPLICATION_JSON_VALUE)
public QueryResults<OrganizationQueryResult> execute() throws ApplicationException
{
return super.getEnrollmentService().findOrganizations();
}
}
ApplicationInitializer.java:
package c.i.i.w.e.config;
@Configuration
@EntityScan("c.i.i.w.e")
@ComponentScan(basePackages = {"c.i.i.w.e"})
public class ApplicationInitializer implements WebApplicationInitializer
{
static final String PU_NAME = "i";
static final String SERVLET_MAPPING = "/";
static final String SERVLET_NAME = "spring";
@Bean(name = "entityManagerFactory")
public LocalEntityManagerFactoryBean entityManagerFactory()
{
LocalEntityManagerFactoryBean entityManagerFactory = new LocalEntityManagerFactoryBean();
entityManagerFactory.setPersistenceUnitName(PU_NAME);
return entityManagerFactory;
}
private void newAppServlet(
ServletContext servletContext,
AnnotationConfigWebApplicationContext appContext)
{
DispatcherServlet dispatcherServlet = new DispatcherServlet(appContext);
ServletRegistration.Dynamic appServlet = servletContext.addServlet(SERVLET_NAME,
dispatcherServlet);
appServlet.setLoadOnStartup(1);
appServlet.addMapping(SERVLET_MAPPING);
}
@Override
public void onStartup(
ServletContext servletContext) throws ServletException
{
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(DispatcherConfig.class);
appContext.setServletContext(servletContext);
appContext.refresh();
servletContext.addListener(new ContextLoaderListener(appContext));
newAppServlet(servletContext, appContext);
}
}
調度程序配置文件
package c.i.i.w.e.config;
@Configuration
@EnableWebMvc
public class DispatcherConfig implements WebMvcConfigurer
{
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer)
{
configurer.enable();
}
@Override
public void configureContentNegotiation(
ContentNegotiationConfigurer configurer)
{
configurer.favorPathExtension(false).favorParameter(true);
}
@Override
public void configurePathMatch(
PathMatchConfigurer configurer)
{
configurer.setUseSuffixPatternMatch(false);
}
@Override
public void addResourceHandlers(
ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(31556926);
}
@Bean
public ViewResolver internalResourceViewResolver()
{
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setSuffix(".jsp");
return bean;
}
}
這是日志文件的最后幾行
2020-08-26 16:13:50,566 DEBUG [org.springframework.web.servlet.DispatcherServlet] GET "/ifactory-enroll/images/ICU_Logo_New_Blue.png", parameters={}
2020-08-26 16:13:50,566 DEBUG [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] Mapped to org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@f3bb1d
2020-08-26 16:13:50,567 DEBUG [org.springframework.web.servlet.DispatcherServlet] Completed 304 NOT_MODIFIED
2020-08-26 16:13:56,047 DEBUG [org.springframework.web.servlet.DispatcherServlet] GET "/ifactory-enroll/secure/json/organizations", parameters={}
2020-08-26 16:13:56,048 DEBUG [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] Mapped to org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@f3bb1d
2020-08-26 16:13:56,049 DEBUG [org.springframework.web.servlet.DispatcherServlet] Completed 404 NOT_FOUND
我不確定哪個更改解決了這個問題,但這是現在有效的最終結果。
應用初始化程序
@Configuration
public class ApplicationInitializer implements WebApplicationInitializer
{
static final String BASE_PACKAGES = "c.i.i.web.enroll";
static final String PU_NAME = "i";
static final String SERVLET_MAPPING = "/";
static final String SERVLET_NAME = "spring";
@Bean(name = "entityManagerFactory")
public LocalEntityManagerFactoryBean entityManagerFactory()
{
LocalEntityManagerFactoryBean entityManagerFactory = new LocalEntityManagerFactoryBean();
entityManagerFactory.setPersistenceUnitName(PU_NAME);
return entityManagerFactory;
}
private void newAppServlet(
ServletContext servletContext)
{
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
DispatcherServlet dispatcherServlet;
ServletRegistration.Dynamic dispatcher;
dispatcherContext.register(DispatcherConfig.class);
dispatcherServlet = new DispatcherServlet(dispatcherContext);
dispatcher = servletContext.addServlet(SERVLET_NAME, dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping(SERVLET_MAPPING);
}
@Override
public void onStartup(
ServletContext servletContext) throws ServletException
{
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);
servletContext.addListener(new ContextLoaderListener(rootContext));
newAppServlet(servletContext);
}
}
調度程序配置文件
@Configuration
@EnableWebMvc
@ComponentScan({"c.i.i.web.enroll"})
public class DispatcherConfig implements WebMvcConfigurer
{
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer)
{
configurer.enable();
}
@Override
public void configureContentNegotiation(
ContentNegotiationConfigurer configurer)
{
configurer.favorPathExtension(false).favorParameter(true);
}
@Override
public void configurePathMatch(
PathMatchConfigurer configurer)
{
configurer.setUseSuffixPatternMatch(false);
}
@Override
public void addResourceHandlers(
ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(31556926);
}
@Bean
public ViewResolver internalResourceViewResolver()
{
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
你本地服務器的端口是什么? 您沒有端口號,例如 http://localhost:portNumber/secure/json/organizations 您可以嘗試 8080 或其他默認值,具體取決於您的本地服務器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.