如何在Web项目中使用Spring 5的RouterFunction?

时间:2018-08-31 09:47:06

标签: java spring spring-boot spring-webflux

问题:提交按钮上,我呼叫 /hello ,但这给了我 HTTP状态404

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

enter image description here

enter image description here

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 功能?

1 个答案:

答案 0 :(得分:0)

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

接下来,您的应用似乎没有带@SpringBootApplication注释的类;如果您想要一个好的起点,则应该使用start.spring.io创建您的应用程序,然后选择webflux启动器。

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

Spring WebFlux不支持JSP,因此无需配置internalviewresolver或在应用程序中添加JSP文件。相反,您应该寻找像胡子,百里香叶等(都列在start.spring.io上)的现代视图技术。

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