【发布时间】: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