带有码头的新功能 Web 框架
New Functional Web Framework with jetty
我想为 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-引导版本:2.0.0.M3
阅读评论,这似乎是依赖带来的问题 spring-boot-starter-web
;如果存在,Spring MVC 应用程序由 Spring Boot.
启动
有一种方法可以明确地告诉Spring启动应用程序的类型,在主应用程序class:
public static void main(String[] args) {
SpringApplication application = new SpringApplication(AgentApplication.class);
application.setWebApplicationType(WebApplicationType.REACTIVE);
application.run(args);
}
我想为 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-引导版本:2.0.0.M3
阅读评论,这似乎是依赖带来的问题 spring-boot-starter-web
;如果存在,Spring MVC 应用程序由 Spring Boot.
有一种方法可以明确地告诉Spring启动应用程序的类型,在主应用程序class:
public static void main(String[] args) {
SpringApplication application = new SpringApplication(AgentApplication.class);
application.setWebApplicationType(WebApplicationType.REACTIVE);
application.run(args);
}