I'm building a RESTful API and have a Spring REST Controller (@RestController) and an annotation-based configuration. I'd like to have my project's welcome-file be a .html or .jsp file with the API documentation.
In other web projects I would place a welcome-file-list in my web.xml, but in this particular project I can't seem to get it to work (preferrably using Java and annotations).
This is my WebApplicationInitializer
public class WebAppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(ApplicationConfig.class);
context.setServletContext(servletContext);
ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatcher",
new DispatcherServlet(context));
dynamic.addMapping("/");
dynamic.setLoadOnStartup(1);
}
}
This is my WebMvcConfigurerAdapter
@Configuration
@ComponentScan("controller")
@EnableWebMvc
public class ApplicationConfig extends WebMvcConfigurerAdapter {
@Bean
public Application application() {
return new Application("Memory");
}
}
And this is a small part of my REST Controller
@RestController
@RequestMapping("/categories")
public class CategoryRestController {
@Autowired
Application application;
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<Integer, Category>> getCategories(){
if(application.getCategories().isEmpty()) {
return new ResponseEntity<Map<Integer, Category>>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<Map<Integer, Category>>(application.getCategories(), HttpStatus.OK);
}
}
So far I've tried:
- Adding just a web.xml with a
<welcome-file-list>
with a<welcome-file>
. (no luck there) - Moving the
@RequestMapping("/categories")
in the Controller from the class level to all of the methods, and adding a new method with@RequestMapping("/")
, which returns either aString
or aModelAndView
with the view name. (the former just returned a blank page with the String, for the latter no mapping could be found) - As suggested here: a combination of both, where my web.xml
<welcome-file>
is "/index", combined with@RequestMapping(value="/index")
returning anew ModelAndView("index")
, and aViewResolver
in my configuration class. (returns aWarning: No mapping found in DispatcherServlet with name 'dispatcher'
, even though "/index" is successfully mapped. Manually adding "/index" to the URL successfully resolves it to index.jsp)