TransWikia.com

Spring 5 MVC not finding mapping to controller returning JSON

Stack Overflow Asked by Gary Kephart on February 16, 2021

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

2 Answers

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

Correct answer by Gary Kephart on February 16, 2021

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.

Answered by Onur Baştürk on February 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP