【发布时间】:2017-08-15 12:13:12
【问题描述】:
我想为New in Spring 5: Functial Web Framework 设置一个示例
于是我设置了RouteConfiguration:
@Configuration
public class RouteConfiguration {
@Autowired
private MyService myService;
@Bean
public RouterFunction<?> routerFunction() {
return route(
GET("/first")
, myService::getItemsFirst)
.and(route(
GET("/second")
, myService::getItemsSecond));
}
}
我使用 jetty 启动了我的应用程序,起初它似乎可以工作......直到我想调用我的一个方法:localhost:8080/first,它返回了一个 404。
我是否定义了我的路由配置错误或为什么路由不可访问?
编辑
使用 netty,您需要提供如下服务器配置:
@Configuration
public class HttpServerConfiguration {
@Autowired
private Environment environment;
@Bean
public HttpServer httpServer(final RouterFunction<?> routerFunction) {
final HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction);
final ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
final HttpServer server = HttpServer.create("localhost", Integer.valueOf(this.environment.getProperty("server.port")));
server.newHandler(adapter);
return server;
}
}
但我在码头找不到类似的东西。
编辑 2
我的依赖:
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencyManagement {
dependencies {
dependency (group: 'org.springframework.cloud', name: 'spring-cloud-starter-consul-discovery', version: '2.0.0.M1')
dependencySet (group: 'org.hibernate', version: '5.2.8.Final') {
entry 'hibernate-core'
entry 'hibernate-entitymanager'
entry 'hibernate-spatial'
}
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-hateoas')
compile('org.springframework.boot:spring-boot-starter-jetty')
compile('org.springframework.boot:spring-boot-starter-webflux') {
exclude module: 'spring-boot-starter-reactor-netty'
}
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-autoconfigure')
compile('org.springframework.boot:spring-boot-actuator')
compile('org.springframework.cloud:spring-cloud-starter-consul')
compile('org.springframework.cloud:spring-cloud-starter-consul-discovery')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('junit:junit')
}
Spring-Boot 版本:2.0.0.M3
【问题讨论】:
-
您有
/first的映射,但前提是请求的内容类型为application/json或application/json+stream。因此,除非您明确指定我非常怀疑浏览器会将其中任何一种作为内容类型发送。因此是 404,因为该组合没有映射。至少我是这么怀疑的。 -
我也可以把 contentType 和 accept 放在代码之外。这不会改变任何事情,请参阅我更新的帖子。
-
启用调试日志(以
--debug开头)并查看是否检测到 bean 并映射路由。确保RouteConfiguration位于 Spring Boot 中的@ComponentScan覆盖的包中。 -
检测到 bean 并且包被
@ComponentScan覆盖。使用码头时需要特殊的服务器配置吗?使用netty,您需要一个配置。找不到任何有关码头的信息。我更新了我的帖子以显示 netty 配置。 -
您必须使用符合 Servlet 3.1 的 Jetty 版本,我相信它在使用时会进行一些自动检测。 (我相信它应该对 Netty 做同样的事情)。然而,所有这些自动配置仍处于里程碑阶段,所以我不能 100% 确定。
标签: spring spring-webflux