这样在代码中移除Router/Handler函数是否正确?(Spring Boot)

huangapple go评论70阅读模式
英文:

Would this be a correct to remove Router/Handler Function in the code (Spring Boot)?

问题

在GitHub上有一个名为Spring Boot Cloud的仓库,其中包含一个简单的路由处理程序示例

@Bean
public RouterFunction<ServerResponse> indexRouter(
        @Value("classpath:/static/index.html") final Resource indexHtml) {

    // 在根目录下提供静态的 index.html,以方便发布消息。
    return route(
            GET("/"),
            request -> ok().contentType(MediaType.TEXT_HTML).bodyValue(indexHtml));
}

请注意,index.html位于一个静态文件夹中,这是由Spring在默认模式下实例化的,因此当访问索引路径("/")时会加载此文件。

我理解有些 Web 服务器可能不会将主路径与 index.html 文件关联起来。但是从 Spring Boot 和 Java 的角度来看,如果在这种情况下(当请求主路径时)不将这样的 Bean 包含到项目中,是否正确?

英文:

There is Spring Boot Cloud repository on GitHub with simple route handler example.

@Bean
public RouterFunction&lt;ServerResponse&gt; indexRouter(
		@Value(&quot;classpath:/static/index.html&quot;) final Resource indexHtml) {

	// Serve static index.html at root, for convenient message publishing.
	return route(
			GET(&quot;/&quot;),
			request -&gt; ok().contentType(MediaType.TEXT_HTML).bodyValue(indexHtml));
}

You can pay attention that index.html is in a static folder which was instantiated by Spring in default mode, so this file is loaded when index path ("/") is requested.

I understand that it is possible that some webservers will not associate main path with index.html file. However from Spring Boot and Java perspective would this be a correct not to include such Bean to the project in this case (when the main path is requested)?

答案1

得分: 0

我已经尝试在不同的上下文中进行测试,包括Spring Boot安全性,一切都运行正常。因此,我关闭这个问题,但如果有人有更广泛的经验,希望能够提供额外的论据。

英文:

I have tried to test it in different contexts including Spring Boot Security and everything works well. So I close this question, however if someone has wider experience appreciate additional arguments.

huangapple
  • 本文由 发表于 2020年9月29日 00:56:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64106465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定