Annotated Controllers

Spring WebFlux 提供基于注释的编程模型,其中 @Controller@RestController 组件使用注释来表示请求映射、请求输入、处理异常等。带注释的控制器具有灵活的方法签名,不必扩展基类,也不必实现特定的接口。

Spring WebFlux provides an annotation-based programming model, where @Controller and @RestController components use annotations to express request mappings, request input, handle exceptions, and more. Annotated controllers have flexible method signatures and do not have to extend base classes nor implement specific interfaces.

以下清单显示了一个基本示例:

The following listing shows a basic example:

  • Java

  • Kotlin

@RestController
public class HelloController {

	@GetMapping("/hello")
	public String handle() {
		return "Hello WebFlux";
	}
}
@RestController
class HelloController {

	@GetMapping("/hello")
	fun handle() = "Hello WebFlux"
}

在前面的示例中,该方法返回要写入响应体的 String

In the preceding example, the method returns a String to be written to the response body.