【问题标题】:New Functional Web Framework with jetty带有码头的新功能 Web 框架
【发布时间】: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/jsonapplication/json+stream。因此,除非您明确指定我非常怀疑浏览器会将其中任何一种作为内容类型发送。因此是 404,因为该组合没有映射。至少我是这么怀疑的。
  • 我也可以把 c​​ontentType 和 accept 放在代码之外。这不会改变任何事情,请参阅我更新的帖子。
  • 启用调试日志(以 --debug 开头)并查看是否检测到 bean 并映射路由。确保 RouteConfiguration 位于 Spring Boot 中的 @ComponentScan 覆盖的包中。
  • 检测到 bean 并且包被 @ComponentScan 覆盖。使用码头时需要特殊的服务器配置吗?使用netty,您需要一个配置。找不到任何有关码头的信息。我更新了我的帖子以显示 netty 配置。
  • 您必须使用符合 Servlet 3.1 的 Jetty 版本,我相信它在使用时会进行一些自动检测。 (我相信它应该对 Netty 做同样的事情)。然而,所有这些自动配置仍处于里程碑阶段,所以我不能 100% 确定。

标签: spring spring-webflux


【解决方案1】:

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

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

public static void main(String[] args) { 
    SpringApplication application = new SpringApplication(AgentApplication.class);
    application.setWebApplicationType(WebApplicationType.REACT‌​IVE);
    application.run(args);
}

【讨论】:

  • 是的,但请记住,仍有一些 spring-boot 工具无法处理反应类型。还有很多事情要做。
猜你喜欢
  • 2022-01-12
  • 1970-01-01
  • 2018-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多