Spring 5 MVC找不到返回JSON的控制器映射



我是Spring 5的新手,我一直在研究关于Spring 5 MVC的所有不同网站,尽可能多地学习,但仍然无法得到对"http://localhost/[webcontext]/secure/json/organizations";。我最后得到的是:[o.s.web.servlet.DispatcherServlet]Completed 404 NOT_FOUND。我可以判断这个类已经加载并且自动布线已经完成,因为我必须修复这些问题。下面是我的代码。我错过了什么或做错了什么?

EnrollmentRestController.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);
}
}

DispatcherConfig.java

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

我不确定是哪个更改修复了这个问题,但以下是现在有效的最终结果。

ApplicationInitializer.java

@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);
}
}

DispatcherConfig.java

@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或其他默认设置,具体取决于您的本地服务器。

最新更新