简体   繁体   English

设置 Spring REST Controller 欢迎文件

[英]Set a Spring REST Controller welcome-file

I'm building a RESTful API and have a Spring REST Controller (@RestController) and an annotation-based configuration.我正在构建一个 RESTful API 并有一个 Spring REST 控制器(@RestController)和一个基于注释的配置。 I'd like to have my project's welcome-file be a .html or .jsp file with the API documentation.我想让我的项目的欢迎文件是带有 API 文档的 .html 或 .jsp 文件。

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).在其他 web 项目中,我会在我的 web.xml 中放置一个欢迎文件列表,但在这个特定的项目中,我似乎无法让它工作(最好使用 Java 和注释)。

This is my WebApplicationInitializer这是我的 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这是我的 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这是我的 REST 控制器的一小部分

@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> .添加一个带有<welcome-file-list><welcome-file>的 web.xml 。 (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 a String or a ModelAndView with the view name.将控制器中的@RequestMapping("/categories")从类级别移动到所有方法,并添加一个带有@RequestMapping("/")的新方法,它返回一个String或一个带有视图名称的ModelAndView (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 a new ModelAndView("index") , and a ViewResolver in my configuration class.正如这里所建议的:两者的组合,其中我的 web.xml <welcome-file>是“/index”,结合@RequestMapping(value="/index")返回一个new ModelAndView("index")和一个ViewResolver在我的配置类中。 (returns a Warning: 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) (返回Warning: No mapping found in DispatcherServlet with name 'dispatcher' ,即使“/index”已成功映射。手动将“/index”添加到 URL 成功将其解析为 index.jsp)

When specifying a controller to handle your index page you should use a @Controller not a @RestController .当指定一个控制器来处理你的索引页面时,你应该使用@Controller而不是@RestController Although the @RestController is a @Controller it doesn't resolve to a view but returns the result as is to the client.尽管@RestController@Controller但它不会解析为视图,而是将结果原样返回给客户端。 When using a @Controller when returning a String it will resolve to the name of a view.在返回String时使用@Controller时,它将解析为视图的名称。

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index() {
        return "index";
    }
}

However there is an easier way to configure this and you don't need a controller for it.但是,有一种更简单的方法来配置它,并且您不需要控制器。 Configure a view controller.配置视图控制器。 In your configuration class simply override/implement the addViewControllers method.在您的配置类中,只需覆盖/实现addViewControllers方法。

public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("index");
}

That way you don't even need to create a class for it.这样你甚至不需要为它创建一个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM