0

我想为Spring 5 中的新功能设置一个示例:功能 Web 框架 所以我设置了一个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));
    }
}

我使用码头开始我的应用程序,起初它似乎工作......直到我想调用我的一个方法: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')
}

弹簧启动版本:2.0.0.M3

4

1 回答 1

1

阅读评论,这似乎是依赖项带来的问题 spring-boot-starter-web;如果存在,则 Spring Boot 将启动 Spring MVC 应用程序。

有一种方法可以在主 Application 类中明确告诉 Spring Boot 应用程序的类型:

public static void main(String[] args) { 
    SpringApplication application = new SpringApplication(AgentApplication.class);
    application.setWebApplicationType(WebApplicationType.REACT‌​IVE);
    application.run(args);
}
于 2017-08-25T08:02:22.487 回答