【问题标题】:How to use RouterFunction of Spring 5 in web project?如何在 Web 项目中使用 Spring 5 的 RouterFunction?
【发布时间】:2018-08-31 09:47:06
【问题描述】:

问题:Submit 按钮上,我调用 /hello 但它给了我 HTTP Status 404

我是 Spring 5 的新手,请帮帮我,我如何转发 /hello 请求。我想实现重要的 Spring 5 特性: (1) 反应式编程支持 (2) 功能性web框架

HelloHandler.java

package com.demo;

import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

public class HelloHandler {
    public  Mono<ServerResponse> handleRequest(ServerRequest serverRequest) {
        return ServerResponse.ok().body(Mono.just("Hello World!"), String.class);
    }
}

SpringAction.java

package com.demo;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

@Configuration
public class SpringAction {

    @Bean
    HelloHandler helloHandler() {
        System.out.println("SpringAction.helloHandler()");
        return new HelloHandler();
    }

    @Bean
    public RouterFunction<ServerResponse> helloRouterFunction(HelloHandler helloHandler) {
        return RouterFunctions.route(GET("/hello"), helloHandler::handleRequest)
                .andRoute(GET("/SpringFunctionalWebFramework/hello"), helloHandler::handleRequest);
    }     
}

SpringFunctionalWebFramework-servlet.xml

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:context = "http://www.springframework.org/schema/context"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:component-scan base-package = "com.demo" />

   <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name = "prefix" value = "/WEB-INF/jsp/" />
      <property name = "suffix" value = ".jsp" />
   </bean>

</beans>

index.jsp

<body>
    <h1>Hello World.</h1>
    <form action="hello" method="GET">
        <input type="submit" value="Submit">
    </form>
</body>

我也想知道是否可以在没有 Spring-boot 的情况下使用 RouterFunction 工具?

【问题讨论】:

    标签: java spring spring-boot spring-webflux


    【解决方案1】:

    您的应用程序依赖于spring-webmvcas described in the Spring Boot reference documentation,这将使您的应用程序成为 Spring MVC 应用程序。所以你应该首先弄清楚是什么带来了这种依赖并将它从你的构建中删除。

    接下来,您的应用似乎没有 @SpringBootApplication 注释类;如果你想要一个好的起点,你应该使用 start.spring.io 创建你的应用并选择 webflux starter。

    接下来,Spring Boot 不需要也不支持-servlet.xml 文件。您应该在其参考文档中阅读有关 Spring Boot 的信息或查看 the Spring guides

    Spring WebFlux 不支持 JSP,因此无需配置内部视图解析器,也无需在应用中添加 JSP 文件。相反,您应该寻找一种现代视图技术,例如 mustache、thymeleaf 等(都在 start.spring.io 上列出)。

    您当然可以在没有 Spring Boot 的情况下使用 Spring WebFlux,but it requires manual setup and deployment(在嵌入式或外部容器中)。我认为 Spring Boot 是迄今为止最简单的方法。

    【讨论】:

    • 谢谢。但我想在我的 web 项目中实现 Spring 5 响应式编程支持功能性 web 框架。所以请给我建议。
    • 已经做到了。从 start.spring.io 创建项目将解决您的大部分问题。
    • 我的要求是我想在 Spring web 项目中实现 Spring 5 的特性。在您看到的问题中,我分享了我的网络项目的屏幕截图。它运行完美,但是当我调用 localhost:8080/SpringFunctionalWebFramework/hello 时,它给了我 404 错误
    猜你喜欢
    • 1970-01-01
    • 2018-04-15
    • 2020-06-12
    • 2011-03-05
    • 2018-09-30
    • 1970-01-01
    • 2017-11-30
    • 2018-06-13
    • 2021-12-29
    相关资源
    最近更新 更多