英文:
Spring 5 MVC not finding mapping to controller returning JSON
问题
以下是你提供的代码的翻译部分:
EnrollmentRestController.java:
@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:
@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:
@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;
}
}
这些是你提供的代码的翻译部分。如果你有更多的问题或需要进一步帮助,请随时问我。
英文:
I'm new to Spring 5, and I've been studying all the different sites about Spring 5 MVC, learning as much as possible, but still can't get a response to "http://localhost/[webcontext]/secure/json/organizations". I end up with this: [o.s.web.servlet.DispatcherServlet] Completed 404 NOT_FOUND. I can tell that the class is loaded and autowiring complete as I had to fix those problems.
Below is my code. What am I missing or doing wrong?
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;
}
}
Here's the last few lines from the log file
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
答案1
得分: 0
以下是翻译的内容:
我不确定哪个修改解决了这个问题,但这里是现在有效的最终结果。
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;
}
}
请注意,以上是您提供的代码的翻译部分,我已按照您的要求进行了翻译。如果您有任何其他问题或需求,请随时告知。
英文:
I'm not sure which changed fixed this, but here's the end results that now work.
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;
}
}
答案2
得分: -1
你的本地服务器端口是多少?你没有像http://localhost:portNumber/secure/json/organizations这样的端口号吗?你可以尝试8080或其他默认端口,具体取决于你的本地服务器。
英文:
What's your local server's port? Don't you have a port number such as http://localhost:portNumber/secure/json/organizations You can try 8080 or other defaults depending on your local server.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论